.NET Regex在IgnorePatternWhitespace才会匹配空格字符

时间:2022-04-11 20:14:51

I have a large regular expression and I've turned on IgnorePatternWhitespace so I can make it more readable. My problem is that I want to match a literal space character. What's the best way to do that?

我有一个很大的正则表达式,我打开了IgnorePatternWhitespace,这样可以使它更容易读。我的问题是我想匹配一个文字空间字符。最好的方法是什么?

An example:

一个例子:

Regex myRegex = new Regex(@"
  (?> <table[^>]*> ) # Find a table
  (?> .*?<tr> ) # Find the first row
  (?> .*?<th> ) # Find the first header column
  My phrase # Look for our key phrase
  etc.
", RegexOptions.IgnorePatternWhitespace);

In the above example, "My phrase" should include a space.

在上面的例子中,“My phrase”应该包含一个空格。

2 个解决方案

#1


11  

Use "\s" or "[ ]"

使用"\s"或"[]"

#2


8  

It would appear that you can simply escape the space character with a backslash:

看起来你可以简单地用反斜线转义空格字符:

My\ phrase

#1


11  

Use "\s" or "[ ]"

使用"\s"或"[]"

#2


8  

It would appear that you can simply escape the space character with a backslash:

看起来你可以简单地用反斜线转义空格字符:

My\ phrase