如前所述,你把你的正则表达式字面空格的正则表达式的部分。除非正则表达式正在扫描的文本中有相同的空格,否则不会获得匹配结果。如果你想使用空格作为你的正则表达式,那么你需要指定RegexOptions.IgnorePatternWhitespace
,之后,如果你想匹配任何空格,你必须明确地这样做,或者通过指定\s
,\x20
等。
需要注意的是,如果你确实指定了RegexOptions.IgnorePatternWhitespace
,那么你可以使用Perl风格的注释(#
来结束行)来记录你的正则表达式(正如我在下面所做的那样)。对于复杂的正则表达式,从现在开始的5年内有人可能是你—! —会感谢你的好意。
我认为你的[推测是意图的]模式比他们需要的更复杂。正则表达式匹配您所指定的标识符的规则是这样的:
[a-zA-Z_][a-zA-Z0-9_]*
分解成它的组成部分:
[a-zA-Z_] # match an upper- or lower-case letter or an underscore, followed by
[a-zA-Z0-9_]* # zero or more occurences of an upper- or lower-case letter, decimal digit or underscore
的正则表达式匹配的数值的常规风格/浮点点文字是这样的:
([+-]?[0-9]+)(\.[0-9]+)?([Ee][+-]?[0-9]+)?
分解成它的组成部分:
( # a mandatory group that is the integer portion of the value, consisting of
[+-]? # - an optional plus- or minus-sign, followed by
[0-9]+ # - one or more decimal digits
) # followed by
( # an optional group that is the fractional portion of the value, consisting of
\. # - a decimal point, followed by
[0-9]+ # - one or more decimal digits
)? # followed by,
( # an optional group, that is the exponent portion of the value, consisting of
[Ee] # - The upper- or lower-case letter 'E' indicating the start of the exponent, followed by
[+-]? # - an optional plus- or minus-sign, followed by
[0-9]+ # - one or more decimal digits.
)? # Easy!
注:一些语法不同如对价值的符号是否是一元运算符或部分的价值 ,以及是否领先+
标志是允许的。语法也各不相同,以 像123245.
是否有效(例如,是没有小数位小数点有效?)
要结合这两个正则表达式,
首先,组他们每个人用括号(您可能要命名含氧基团,像我那样):
(?<identifier>[a-zA-Z_][a-zA-Z0-9_]*)
(?<number>[+-]?[0-9]+)(\.[0-9]+)?([Ee][+-]?[0-9]+)?
接下来,结合日Ë交替操作,|
:
(?<identifier>[a-zA-Z_][a-zA-Z0-9_]*)|(?<number>[+-]?[0-9]+)(\.[0-9]+)?([Ee][+-]?[0-9]+)?
最后,附上整个事情在@“......”字面,你应该是好去。
这就是关于它的一切。
文本格式是否与您的文章完全相同?它是否逗号分隔?或者''字词'之间还有另一个分隔符? – jmstoker
对不起,逗号不是输入的一部分。他们只是用来分隔我上面例子中的模式。 – Jonathan
您可以举一个原始文本如何格式化的例子吗? – jmstoker