简体   繁体   English

正则表达式匹配数字之间的特殊字符

[英]Regex matching for special characters between numbers

Condition: 1 to 2 digit number followed by optional [. , ” ']条件:1 到 2 位数字后跟可选的[. , ” '] [. , ” '] + 1 to 4 digit number [. , ” '] + 1 到 4 位数字

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.您不需要字符 class 中的空格,也不必转义字符。

Matching optional digits \d{0,4} could possibly also match 13.匹配可选数字\d{0,4}也可能匹配13.

You could make the second part optional inclusing the character class and the digits and use a quantifier +您可以将第二部分设为可选,包括字符 class 和数字并使用量词+

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

Regex demo正则表达式演示

If the M should be present, you could use a capturing group如果 M 应该存在,您可以使用捕获组

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

Regex demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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