Clement Martino
Clement Martino

Reputation: 475

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

Upvotes: 4

Views: 80

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

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

vks
vks

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

m.cekiera
m.cekiera

Reputation: 5395

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

Upvotes: 1

Related Questions