简体   繁体   中英

java regex matching each group starting with specific string

I have a string like a1wwa1xxa1yya1zz .

I would like to get every groups starting with a1 until next a1 excluded. (In my example, i would be : a1ww , a1xx , a1yy and a1zz

If I use :

Matcher m = Pattern.compile("(a1.*?)a1").matcher("a1wwa1xxa1yya1zz");
while(m.find()) {
  String myGroup = m.group(1);
}

myGroup capture 1 group every two groups.
So in my example, I can only capture a1ww and a1yy .

Anyone have a great idea ?

Split is a good solution, but if you want to remain in the regex world, here is a solution:

Matcher m = Pattern.compile("(a1.*?)(?=a1|$)").matcher("a1wwa1xxa1yya1zz");
while (m.find()) {
  String myGroup = m.group(1);
  System.out.println("> " + myGroup);
}

I used a positive lookahead to ensure the capture is followed by a1 , or alternatively by the end of line.

Lookahead are zero-width assertions, ie. they verify a condition without advancing the match cursor, so the string they verify remains available for further testing.

You can use split() method, then append "a1" as a prefix to splitted elements:

String str = "a1wwa1xxa1yya1zz";
String[] parts = str.split("a1");
String[] output = new String[parts.length - 1];

for (int i = 0; i < output.length; i++)
    output[i] = "a1" + parts[i + 1];

for (String p : output)
    System.out.println(p);

Output:

a1ww
a1xx
a1yy
a1zz

I would use an approach like this:

    String str = "a1wwa1xxa1yya1zz";
    String[] parts = str.split("a1");
    for (int i = 1; i < parts.length; i++) {
        String found = "a1" + parts[i];
    }

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