1

I have this code:

String line = "There were bear#67 with dog#1323 and cat#5475 in the forest";
String pattern = ".* ([^ ]+)#(\\d{4}).*";

Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find( )) {
    System.out.println("Found value: " + m.group(0) );
    System.out.println("Found value: " + m.group(1) );
    System.out.println("Found value: " + m.group(2) );
}  else {
    System.out.println("NO MATCH");
}

It prints

Found value: There were bear#67 with dog#1323 and cat#5475 in the forest
Found value: cat
Found value: 5475

However I need to get all matches as ArrayList:

dog#1323, cat#5475

How would I do that?

3
  • What did you mean by However I need to get all matches as ArrayList ? Commented Oct 30, 2020 at 16:52
  • 1
    your regex should be String pattern = "[^ ]+#\\d{4}"; and add matcher.group(0) to a list inside the loop Commented Oct 30, 2020 at 16:54
  • @YCF_L That resulting arraylist need to consist of two elements: String dog#1323, and String cat#5475 Commented Oct 30, 2020 at 16:56

3 Answers 3

1

Converting my comment to answer so that solution is easy to find for future visitors.

You may use a simpler regex without any capture group:

[^ ]+#\\d{4}

Which matches 1+ of a non-space characters before # and then 4 digits.

Code:

final String line = "There were bear#67 with dog#1323 and cat#5475 in the forest";

final Pattern r = Pattern.compile("[^ ]+#\\d{4}");    
Matcher m = r.matcher(line);

List<String> arr = new ArrayList<>();

while (m.find( )) {
   arr.add(m.group());
}

System.out.println(arr);
//=> [dog#1323, cat#5475]
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to put all matches in a list, then you have to create one like this :

String pattern = "[^ ]+#\\d{4}"; // or "[a-z]+#\\d{4}" if you want only alphabets
//..
List<String> values = new ArrayList<>();
while (m.find()) {
    values.add(m.group(0));
}

Note: you need to use while loop and not an if statement(which match only one value).


Output

[dog#1323, cat#5475]

Comments

1

Using the Stream API, you can do it with a single statement.

Demo:

import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<String> list = Pattern.compile("(?<=\\s?)\\p{L}+#\\d{4}")
                                .matcher("There were bear#67 with dog#1323 and cat#5475 in the forest")
                                .results()
                                .map(MatchResult::group)
                                .collect(Collectors.toList());
        
        System.out.println(list);
    }
}

Output:

[dog#1323, cat#5475]

Explanation of the regex:

  1. (?<=\s?) specifies the positive lookbehind for optional whitespace
  2. \p{L}+ specifies one or more letter(s)
  3. # specifies the char, #
  4. \d{4} specifies 4 digits

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.