Reputation: 9
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
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
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
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));
}
Upvotes: 0
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]
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
Reputation: 7361
try this regex:
: (.*?)(?:Paper size|$) //global
regex demo output:
Upvotes: 2