如前所述,你把你的正則表達式字面空格的正則表達式的部分。除非正則表達式正在掃描的文本中有相同的空格,否則不會獲得匹配結果。如果你想使用空格作爲你的正則表達式,那麼你需要指定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