简体   繁体   English

.NET正则表达式空格特殊字符

[英].NET Regular Expression white space special characters

This pattern is not working sometimes (it works only for the 3rd instance). 该模式有时不起作用(仅适用于第三个实例)。 The pattern is ^\\s*flood\\s{55}\\s+\\w+ 模式为^\\s*flood\\s{55}\\s+\\w+

I am new to regular expression and I am trying to write a regular expression that captures all the following conditions: 我是正则表达式的新手,我试图编写一个捕获以下所有条件的正则表达式:

Example 1: flood                 a)
Example 2: flood                 As respects
Example 3: flood                 USD100,000

(it's in a tabular format and there's a lot of space between flood and the next word) (采用表格格式,在下一个单词和下一个单词之间有很多空格)

Your expression is saying: 您的表情是说:

^\\s* The start of the string may have zero or more whitespace characters ^\\s*字符串的开头可能有零个或多个空格字符

flood followed by the string flood flood然后是串flood

\\s{55} followed by exactly 55 whitespace characters \\s{55}后面紧跟55个空格字符

\\s+\\w+ followed by one or more whitespace characters and then one or more word characters. \\s+\\w+然后是一个或多个空格字符,然后是一个或多个文字字符。

If you want a minimum number of whitespace characters, say at least 30, followed by one or more word chraracters, then you could do this: 如果您需要最少数量的空白字符,例如至少30个字符,然后是一个或多个单词书写器,则可以执行以下操作:

^\\s*flood\\s{30,}\\w+

Try this: 尝试这个:

string input =
@" flood                 a)
   flood                 As respects
   flood                 USD100,000";
string pattern = @"^\s*flood\s+.+$";
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Multiline);

If there are a lot of spaces between flood and the next word you could omit \\s{55} which is a quantifier that matches a whitespace character 55 times. 如果泛洪和下一个单词之间有很多空格,则可以省略\\s{55} ,它是一个与空白字符匹配55次的量词。

That would leave you with ^\\s*flood\\s+\\w+ which does not yet match all the values at the end because \\w matches a word character but not a whitespace or any of ), . 这将使你^\\s*flood\\s+\\w+这还没有结束时,因为匹配所有值\\w字字符,但不是一个空格或任何相匹配),

To match your values you might use a character class and add the characters that you allow to match: 为了匹配您的值,您可以使用字符类并添加允许匹配的字符:

^\\s*flood\\s+[\\w,) ]+

Or if you want to match any character you could use a dot instead of a character class. 或者,如果要匹配任何字符,则可以使用点而不是字符类。

According to your comment, you might use a positive lookbehind: 根据您的评论,您可以在后面加上积极的印象:

(?<=\\(13\\. Deductible\\))\\s*(\\s*flood\\s+[\\w,) ]+)+

Demo 演示

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

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