Combine all numeric characters without letters or letters with an accent before or after

I try this:

\b\d+\b 

but for this line:

 0225 : appt, (parking) niv -2 0015_1 5étage sqdqs25485 7871sdd 

I want to find:

 0225 2 0015 1 
+4
source share
3 answers
 (?<![\p{M}\p{L}\d])\d+(?![\p{M}\p{L}\d]) 

You can achieve this in this way. Watch the demo.

https://regex101.com/r/fM9lY3/24

+2
source

Try:

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

Where:

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

Demo

+1
source

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.

+1
source

All Articles