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()) {
	arr.add(m.group(1));  
	count++;
}		
out_arr = arr.toArray(new String[0]);
out_ArrSize = count;
List<Integer> arrInt = new ArrayList<Integer>();
for(String val: arr){
	arrInt.add(val.length());
}
out_ArrInt = arrInt.toArray(new Integer[0]);

If we call a 5 digit number “Y” and a non-numeral X, your pattern matches:
XYX
your input string is
...XYXYXYXYXYXYXYXYX...
if your input string were
...XYXXYXXYXXYXXYXXYXXYX...
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
XXXXXXXXYXYXYXYXYX...X
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 https://www.freeformatter.com/java-regex-tester.html and https://regexr.com/

3 Likes

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