简体   繁体   中英

Match all occurrences Regex Java

i'd like to recognize all sequences of "word-number-word" of a string with Regex Java API.

For example, if i have "ABC-122-JDHFHG-456-MKJD", i'd like the output : [ABC-122-JDHFHG, JDHFHG-456-MKJD].

String test = "ABC-122-JDHFHG-456-MKJD";

Matcher m = Pattern.compile("(([A-Z]+)-([0-9]+)-([A-Z]+))+")
            .matcher(test);
while (m.find()) {
    System.out.println(m.group());
}

The code above return only "ABC-122-JDHFHG".

Any ideas ?

The last ([AZ]+) matches and consumes JDHFHG , so the regex engine only "sees" -456-MKJD after the first match, and the pattern does not match this string remainder.

You want to get "whole word" overlapping matches.

Use

String test = "ABC-122-JDHFHG-456-MKJD";

Matcher m = Pattern.compile("(?=\\b([A-Z]+-[0-9]+-[A-Z]+)\\b)")
            .matcher(test);
while (m.find()) {
    System.out.println(m.group(1));
} // => [ ABC-122-JDHFHG, JDHFHG-456-MKJD ]

See the Java demo

Pattern details

  • (?= - start of a positive lookahead that matches a position that is immediately followed with
    • \\\\b - a word boundary
    • ( - start of a capturing group (to be able to grab the value you need)
    • [AZ]+ - 1+ ASCII uppercase letters
    • - - a hyphen
    • [0-9]+ - 1+ digits
    • - - a hyphen
    • [AZ]+ - 1+ ASCII uppercase letters
  • ) - end of the capturing group
  • \\\\b - a word boundary
  • ) - end of the lookahead construct.

Here you go, overlap the last word.
Make an array out of capture group 1.

Basically, find 3 consume 2. This makes the next match position start
on the next possible known word.

(?=(([AZ]+-\\d+-)[AZ]+))\\2

https://regex101.com/r/Sl5FgT/1

Formatted

 (?=                           # Assert to find 
      (                             # (1 start), word,num,word
           (                             # (2 start), word,num
                [A-Z]+ 
                -
                \d+ 
                -
           )                             # (2 end)
           [A-Z]+ 
      )                             # (1 end)
 )
 \2                            # Consume word,num

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