1

I'm trying to figure out a Java regex problem that I'm having. This is 1.6, but I don't think that matters. Anyway...

I'm going to have some input data like the following...

"Blah yadda yidda 44-Barack Obama, this that the other"

or

"Something here, there 22-Hyphenated-example.  Hi there folks"

Basically I want to extract everything following the number through to the trailing punctuation. In the two examples inputs I want to extract...

"Barack Obama"

and

"Hyphenated-example"

I can't quite get the pattern I need to use. The closest I can get is this...

"[0-9]{1,2}-([A-Z -]*\\b*)"

that, however, gives me...

"44-Barack Obama"

My code is...

Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("Blah yadda yidda 44-Barack Obama, this that the other");
if (matcher.find())
    // This gives me "44-Barack Obama" but I want "Barack Obama".
    System.out.println(matcher.group());

Interestingly, I'm using the QuickREx Eclipse plugin to test this pattern and it returns the proper value. However, running the above code does not.

Any ideas?

1 Answer 1

4

Use the result of matcher.group(1), not matcher.group(). The second form returns everything that was matched by the previous matches or find method. The first form is used to access individual capture groups in the regex.

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

1 Comment

Ouch!! I guess it's important to read the ENTIRE Javadoc! Erickson wins the prize. In under a minute even!!

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.