Java – regular expressions extract text in reverse order up to the third character instance

I have a format of XXXX_ YYYY_ YYYYYYY_ String of yyyyyzzz

How to extract a string from the back until it hits thrid_ (underline) Extracted value: YYYY_ YYYYYYY_ YYYYYYZZZZ

I tried this (?: [^] *) {3} ) $, which seems to start with an additional, I can delete it in Java

Is there any way I can get

Solution

like this:

String line = "XXXX_YYYY_YYYYYYY_YYYYYYZZZZ";

        Pattern p = Pattern.compile("([^_]+(?:_[^_]*){2})$");
        Matcher m = p.matcher(line);
        if(m.find()) {
            System.out.println(m.group(1));
        }

Just divide the "three times" {3} into one_ And two instances that need it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>