0

if I have a text

R 26 bla bla bla bla R 25 bla bla R 25/30 bla bla bla S 30/50/30 bla bla

and I have a regex

[RS] (\\d+|\\d+/\\d+|\\d+/\\d+/\\d+) 

which will match the bold data ... now , I want a code that returns for me the bold expressions + the following data.

For example , I want the following pairs:

R 26 : bla bla bla bla
R 25 : bla bla
R 25/30 : bla bla bla
S 30/50/30 : bla bla

may be this image will be clearer :

2
  • 1
    " I want a code " - is that a 'plz sendz the codez' variant? Commented Mar 20, 2011 at 1:06
  • See this meta faq answer Commented Mar 20, 2011 at 4:12

2 Answers 2

2

Here you go!

Pattern pattern = Pattern.compile("([RS] (\\d+|\\d+/\\d+|\\d+/\\d+/\\d+)) ([^RS]*)");

Matcher matcher = pattern.matcher("R 26 bla bla bla bla R 25 bla bla R 25/30 bla bla bla S 30/50/30 bla bla");

while(matcher.find()) {
    System.out.println(matcher.group(1) + " " + matcher.group(3));
}
Sign up to request clarification or add additional context in comments.

2 Comments

This implements the Java code but the regex is the same as the original which does not work.
@ridgerunner - I only paste code once I verify that they work. The orignal RegEx had an escaping issue. This one is different.
0

The order of the alternatives is important. With your original regex, the second and third alternatives will never match. Reversing their order fixes the problem like so:

Pattern regex = Pattern.compile("([RS] (\\d+/\\d+/\\d+|\\d+/\\d+|\\d+)) ([^RS]*)");

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.