简体   繁体   English

具有多个条件的正则表达式

[英]Regular expression with multiple conditions

I'm trying to write a regular expression for checking numbers. 我正在尝试编写一个用于检查数字的正则表达式。

I should be able to identify the following: 我应该能够识别以下内容:

  • 999 999
  • 99.99 99.99
  • 99,99 99,99
  • 99 9/9 99 9/9

But not the following 99M, 99BN, 99$. 但以下99M,99BN,99 $除外。

So I thought this might work: string rule1 = @"\\b[0-9]+(?(.|,|\\n){0,1}[0-9]+)\\b"; 因此,我认为这可能可行: string rule1 = @"\\b[0-9]+(?(.|,|\\n){0,1}[0-9]+)\\b";

[0-9]+ the first part says: I need 1 or more digits. [0-9]+第一部分说:我需要一个或多个数字。

(?(.|,|\\n){0,1}[0-9]*) In this second part my intention was to express a condition: (?(.|,|\\n){0,1}[0-9]*)在第二部分中,我的目的是表达一个条件:

If a point or a comma or a space comes after the first part then I need one or more numbers after the point, comma or space. 如果在第一部分之后出现点,逗号或空格,则在该点,逗号或空格之后需要一个或多个数字。 But since I don't know a lot about regular expressions I'm stuck and getting a run-time exception. 但是由于我对正则表达式了解不多,所以我陷入了困境并遇到了运行时异常。

I'm not really sure how you want to handle the last case, since as I said in a comment, there's a space in your example. 我不确定您要如何处理最后一种情况,因为正如我在评论中所说,您的示例中有空格。 But assuming that was supposed to be a line break, I believe something like this should work. 但是假设这应该是换行符,那么我相信类似的方法应该可行。

(\b\d+(\.|,|\/)?\d+\b)[^$]

Broken down: 细分:

(
\b           Word break
\d+          Any digit character (equivalent to your [0-9]), matched one or more times
(\.|,|\/)?   A period (escaped), comma, or slash (escaped), matched zero or one time.
\d+          Any digit character (equivalent to your [0-9]), matched one or more times
\b           Word break
)
[^$]         Avoid the last case, of 99$

I've tested this to the extent I can here . 我测试过这程度,我可以在这里

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

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