1

The snippet below should return and store matches in the ArrayList but for some reason, I get only the last match, that is 23; I would expect 256 and 23 of course. Could you tell me what I'm doing wrong? Thanks in advance.

public static void main(String[] args){ String target = "nawaK256he23llo"; String regexNb = "[0-9]+"; Pattern patternNb = Pattern.compile(regexNb); Matcher mNb = patternNb.matcher(target); List<String> allMatchesNb = new ArrayList<String>(); while (mNb.find()) { System.out.println("mNb matched : " + mNb.find()); allMatchesNb.add(mNb.group()); for (String nb : allMatchesNb) { System.out.println("content of ArrayList :" + nb); } } } 

Output is the following :

content of ArrayList : 23 
2
  • 10
    Your loop calls find() twice at each iteration, once in while and once in println. Get rid of the second call . Commented Jun 2, 2017 at 9:03
  • 1
    Yes I've overlooked that, thanks ! Commented Jun 2, 2017 at 9:15

3 Answers 3

3

To avoid this problem, and don't change the logic of your code, you can use :

boolean find = false;//create a variable so you can use in the loo^p while (find = mNb.find()) { // ^-------------------Initialize the variable System.out.println("mNb matched : " + find); //------------------------------------------^^ 

Note

It should be one equal = in find = mNb.find() not two, this is not a condition it is an initialization, so you can use it in System.out.println("mNb matched : " + find);

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

Comments

3

you are calling find() twice and that is consuming the 1st match

System.out.println("mNb matched : " + mNb.find()); 

do instead:

String target = "nawaK256he23llo"; String regexNb = "[0-9]+"; Pattern patternNb = Pattern.compile(regexNb); Matcher mNb = patternNb.matcher(target); List<String> allMatchesNb = new ArrayList<>(); while (mNb.find()) { allMatchesNb.add(mNb.group()); } System.out.println(allMatchesNb); 

Comments

1

From the javadoc of Matcher.find() (emphasis by me):

Attempts to find the next subsequence of the input sequence that matches the pattern.

This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

You are calling mNb.find() twice in each iteration, the first one finds "256", the second one "23". The easiest solution would be to get rid of one invocation:

public static void main(String[] args) { String target = "nawaK256he23llo"; String regexNb = "[0-9]+"; Pattern patternNb = Pattern.compile(regexNb); Matcher mNb = patternNb.matcher(target); List<String> allMatchesNb = new ArrayList<String>(); while (mNb.find()) { System.out.println("mNb matched : " + true); allMatchesNb.add(mNb.group()); for (String nb : allMatchesNb) { System.out.println("content of ArrayList :" + nb); } } } 

Outputs:

mNb matched : true content of ArrayList :256 mNb matched : true content of ArrayList :256 content of ArrayList :23 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.