import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\b\\d+(?:,\\d+)*";
final String string = "this is number 88 and rest of the text\n"
+ "this is number 2,563 and rest of the text\n\n"
+ "after I export it into excel. Problem is that I'm using custom tool (not programming language ) that excepts text as an input, regex pattern and export the output. This is why I need single pattern that eliminate space and produce either 88 or 2563 or any other number. The commas are every 3 positions obviously, so it can also be 1,345,321 as well as just single number 5 between \"this is number .... and rest of the text\"";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html