0

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

3
  • regexr.com/3dfoa - ([A-Z]\d) Commented May 23, 2016 at 21:24
  • 2
    Depends on what you can have besides An. Commented May 23, 2016 at 21:25
  • 1
    s.replaceAll("^Paper size: ", "").split("Paper size: "). Commented May 23, 2016 at 21:26

5 Answers 5

2

try this regex:

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

regex demo output:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

String s = "Loan Postpone - 7 day, pay: 24Loan Postpone - 7f7 day, pay: 25Loan Postpone - 7-9 day, pay: 24.1"; How can I extract from here (7,24) (7f7, 25) and (7-9,24.1) pairs?
Giorgi, maybe something like -\s(.*?)\sday,\spay:\s(.*?)(?:L|$) regex101.com/r/gF9pJ2/1
0

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.

Comments

0
  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

Comments

0

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.

Comments

0

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.