Reputation: 475
I try this:
\b\d+\b
but for this string:
0225 : appt, (parking) niv -2 0015_1 5étage sqdqs25485 7871sdd
I want to find:
0225 2 0015 1
Upvotes: 4
Views: 80
Reputation: 626747
You can use the following code to obtain 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));
}
See IDEONE demo
The regex means match 1 or more digits (\d+
) only if they are preceded with _
or a word boundary ((?<=_|\\b)
) and followed by a word boundary or an underscore ((?=\\b|_)
).
Use (?U)
flag (or Pattern.UNICODE_CHARACTER_CLASS
), since \b
without (?U)
flag is broken.
Upvotes: 1
Reputation: 67968
(?<![\p{M}\p{L}\d])\d+(?![\p{M}\p{L}\d])
You can achieve it this way.See demo.
https://regex101.com/r/fM9lY3/24
Upvotes: 2