Giorgi Kacadze
Giorgi Kacadze

Reputation: 9

Split repeated string by regex java

From That

    String s = "Paper size: A4Paper size: A3Paper size: A2"; 

I need to get A4, A3 and A2. How can I do that?

    String regex = "Paper size: (.*?)";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(s);

    while(m.find()){        
        System.out.println( m.group(1));
    }

this return empty strings.

edit: at the place of the A3,A4,A5 may be any char sequence, which then next "Paper size" indicates to continue the next group

Upvotes: 0

Views: 117

Answers (5)

jal bal
jal bal

Reputation: 15

If you just need to get Paper size: A4 Paper size: A3 Paper size: A2 use String regex = "Paper size: A[234]";

System.out.println(m.group());

Upvotes: 0

JanLeeYu
JanLeeYu

Reputation: 1001

Just simply replace your regex with this one : "Paper size: (..)?"

OR

"Paper size: (\\w\\d)?"

if you wanted to be strict that the captured group always consists of a letter followed by a number.

Upvotes: 0

Hosseini
Hosseini

Reputation: 557

  String s = "Paper size: A4Paper size: A3Paper size: A2";
  String regex = "([A-Z]\\d)";

  Pattern p = Pattern.compile(regex);
  Matcher m = p.matcher(s);

  while(m.find()){
     System.out.println( m.group(1));
  }

enter image description here

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626950

If your values can have any values, you may use a workaround splitting:

String s = "Paper size: A4Paper size: A3Paper size: A2";
String[] res = s.replaceFirst("^Paper size:\\s*", "") // Remove the first delimiter to get rid of the empty value
                .split("Paper size:\\s*");            // Split
System.out.println(Arrays.toString(res)); // => [A4, A3, A2]

See an IDEONE demo

Or, you can match any text other than Paper size: and capture it with ([^P]*(?:P(?!aper size:)[^P]*)*):

String s = "Paper size: A4Paper size: A3Paper size: A2";
String pattern1 = "Paper size: ([^P]*(?:P(?!aper size:)[^P]*)*)";
Pattern ptrn = Pattern.compile(pattern1);
Matcher matcher = ptrn.matcher(s);
List<String> res = new ArrayList<>();
while (matcher.find())
    res.add(matcher.group(1));
System.out.println(res); // => [A4, A3, A2]

See another IDEONE demo

The Paper size: ([^P]*(?:P(?!aper size:)[^P]*)*) is actually the same pattern as (?s)Paper size: (.*?)(?=Paper size: |\z), but an unrolled one, a much more efficient with longer inputs.

Upvotes: 0

Scott Weaver
Scott Weaver

Reputation: 7361

try this regex:

: (.*?)(?:Paper size|$) //global

regex demo output:

enter image description here

Upvotes: 2

Related Questions