You can use the following code to get the required numbers:
String s = "0225 : appt, (parking) niv -2 0015_1 5étage"; Pattern pattern = Pattern.compile("(?<=_|\\b)\\d+(?=\\b|_)", Pattern.UNICODE_CHARACTER_CLASS); Matcher matcher = pattern.matcher(s); while (matcher.find()){ System.out.println(matcher.group(0)); }
Watch the IDEONE demo
A regular expression matches 1 or more digits ( \d+ ) only if it is preceded by _ or the word boundary ( (?<=_|\\b) ) and then the word boundary or underscore ( (?=\\b|_) )
Use the flag (?U) (or Pattern.UNICODE_CHARACTER_CLASS ), since the \b flag without (?U) broken.
source share