简体   繁体   中英

How to match everything up to the last occurence of a special character set?

I'm trying to modify my regex splitter.

My expression is simple: match one or more occurences of a characterset: \\s*[|()!=\\s]+

Running over this example would give:

(output !==(not output)) | (output)
//output, not output, output

Now I want the following: "if a = character occures, then behind match everything behind up to the last occurence of again the same character set."

Again looking at the example above, it can see the first = , and should then match the following completely: !==(not output)) | ( !==(not output)) | (

How could this be done?

I am not sure if that is what you mean for later use but for your current input it works

String data = "(output !==(not output)) | (output)";
Matcher m = Pattern.compile("(\\w+)\\s*(?=!?==)(.*)\\1").matcher(data);
//                           ^-group1          ^   ^-repeated group1
//                                             |group 2
while (m.find()) {
    System.out.println(m.group(2));//match from group 2
}

output:

!==(not output)) | (

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM