1

I have this code and I want to find both 1234 and 4321 but currently I can only get 4321. How could I fix this problem?

String a = "frankabc123 1234 frankabc frankabc123 4321 frankabc";
String rgx = "frank.* ([0-9]*) frank.*";
Pattern patternObject = Pattern.compile(rgx);
Matcher matcherObject = patternObject.matcher(a);
while (matcherObject.find()) {
    System.out.println(matcherObject.group(1));
}
4
  • 1
    Your regex should be frank.*? ([0-9]*) frank, otherwise, the .* will eat up everything. Commented Mar 31, 2014 at 21:38
  • Learn about lazy vs greedy quantifiers. Commented Mar 31, 2014 at 21:39
  • To elaborate on what @nhahtdh said, * is greedy by default, which means it will match the largest amount possible. Adding a ? to it makes it non-greedy. Commented Mar 31, 2014 at 21:39
  • Does 'frank' has to be part of the regexp or do you only want to get only "number" words? Commented Mar 31, 2014 at 21:43

2 Answers 2

2

Your regex is too greedy. Make it non-greedy.

String rgx = "frank.*? ([0-9]+) frank";
Sign up to request clarification or add additional context in comments.

Comments

1

Your r.e. is incorrect. The first part: frank.* matches everything and then backtracks until the rest of the match succeeds. Try this instead:

String rgx = "frank.*? ([0-9]*) frank";

The ? after the quantifier will make it reluctant, matching as few characters as necessary for the rest of the pattern to match. The trailing .* is also causing problems (as nhahtdh pointed out in a comment).

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.