简体   繁体   English

启用IgnorePatternWhitespace时,.NET正则表达式匹配空格字符

[英].NET Regex match space character when IgnorePatternWhitespace is on

I have a large regular expression and I've turned on IgnorePatternWhitespace so I can make it more readable. 我有一个很大的正则表达式,我已经打开了IgnorePatternWhitespace,所以我可以让它更具可读性。 My problem is that I want to match a literal space character. 我的问题是我想匹配一个文字空格字符。 What's the best way to do that? 最好的方法是什么?

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. 在上面的例子中,“我的短语”应该包含一个空格。

使用“\\ s”或“[]”

看起来您可以使用反斜杠简单地转义空格字符:

My\ phrase

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

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