简体   繁体   中英

Regex matching for special characters between numbers

Condition: 1 to 2 digit number followed by optional [. , ” '] [. , ” '] + 1 to 4 digit number

Examples:

7 M
13.6 M
8.205m
9.,56m

Expected Results:

7
13.6
8.205
9.,56

Regex Pattern I've tried:

(?:^\d{1,2})(?:[\.\,\’\"]{0,2})\d{0,4} 

This doesn't work as expected. Any suggestions?

You don't need the spaces in the character class and you don't have to escape the chars as well.

Matching optional digits \d{0,4} could possibly also match 13.

You could make the second part optional inclusing the character class and the digits and use a quantifier +

^\d{1,2}(?:[.,”’]+\d{1,4})?

Regex demo

If the M should be present, you could use a capturing group

^(\d{1,2}(?:[.,”’]+\d{1,4})?) ?[Mm]$

Regex demo

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