我不太精通正则表达式。我不得不使用它们,也许每隔几年一次,而且主要用于类(class)作业。不管怎样,对于任何熟悉正则表达式的人来说,下面的问题应该是一个相当直接的问题/答案。
我需要确保输入到字段中的文本遵循以下格式:
x y z
or
x,y,z
or
x y z / <same pattern can repeat for almost unlimited length>
or
x,y,z / <...> // Spaces after the comma are ok
where x, y and z can only be integers. The patterns cannot be intermixed, so you cannot have
x, y, z / x y z / ...
I've tried the following
([1-9] [1-9] [1-9])
获取 x y z 部分,但我不知道如何包含 '/' 和 ','
有什么建议吗?
最佳答案
尝试将正则表达式分解成多个部分。然后尝试将它们结合起来。
例如,像 1024
这样的整数是一个或多个数字的序列,即 [0-9]+
。等等
语法:
digit ::= [0-9]
space ::= [ ]
slash ::= [/]
comma ::= [,]
integer ::= digit+
separator ::= space | comma
group ::= integer separator integer separator integer
group-sep ::= space slash space
groups ::= group ( group-sep group )*
正则表达式:
([0-9]+[ ,][0-9]+[ ,][0-9]+)([ ][/][ ][0-9]+[ ,][0-9]+[ ,][0-9]+)*
关于c# - 如何为给定示例制定正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3806019/