Java Regular Expression, extract 5 digit numbers from a string

String Input looks like : C:\FW INV ORDER - 35425 35426 35427 35428 35429 35430 35431 35432.msg
My output looks like:

  • arr: [35425, 35427, 35429, 35431]
  • Arr Size: 4

So why is it not extracting all 5 digit numbers?

List<String> arr = new ArrayList<String>();
Pattern p = Pattern.compile("\\D(\\d{5})\\D");
Matcher m = p.matcher(c_Filepath);
int count = 0;
while (m.find()) {
out_arr = arr.toArray(new String[0]);
out_ArrSize = count;
List<Integer> arrInt = new ArrayList<Integer>();
for(String val: arr){
out_ArrInt = arrInt.toArray(new Integer[0]);

If we call a 5 digit number “Y” and a non-numeral X, your pattern matches:
your input string is
if your input string were
then you would get the results you are expecting.

Is there a way to do it without using Java? Replace all chars that are not numbers by a “,”

This isn’t a java problem - it’s defining your regex correctly; i don’t know the homogeneity of the patterns in your data, but if they are like the input line where you have
then dump the first \\D in your pattern.
There are some great regex resources online so your can figure these things out for yourself - like and


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.