简体   繁体   中英

Match all numeric characters without letters or accented letters before or after

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
(?<![\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

Try with:

(?<![\p{L}\d])(\d+)(?![\p{L}\d])

where:

  • (?<![\\p{L}]) - negative lookbehind for single code point in the category "letter",
  • (\\d+) - one or more digits,
  • (?![\\p{L}]) - negative lookahead for single code point in the category "letter",

DEMO

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM