2009-06-10 15 views
8

我有一個很大的正則表達式,並且已打開IgnorePatternWhitespace,因此我可以使其更具可讀性。我的問題是我想匹配一個字面空格字符。什麼是最好的方式來做到這一點?當IgnorePatternWhitespace打開時,.NET正則表達式匹配空格字符

一個例子:

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); 

在上述示例中, 「我的短語」 應包括一個空間。

回答

7

這樣看來,你可以簡單地使用反斜線空格字符:

My\ phrase 
11

使用「\ s」或「[]」

+5

使用[]由於\ S也將匹配的標籤和換行。 – 2009-06-10 20:53:44