与换行符

与换行符

问题描述:

提取文本我使用,以提取<p>标签内的文本这种模式与换行符

const string ptnBodytext = @"<p>\s*(.+?)\s*</p>"; 

。它工作正常,除了新行的文字,例如:

<p> 
    Lorem ipsum 
    second line or 
    third one? 
</p> 

我怎样才能改变模式以包括新行,制表符等等?

+0

如果你解析HTML:http://stackoverflow.com/questions/56107/what-is-the-best-way-to-parse-html-in-c –

+0

那么,为什么不使用'.Replace( “

”,“”)。替换(“

”,“”)'? –

你要么需要激活DOTALL模式或:

const string ptnBodytext = @"<p>([\s\S]+?)</p>"; 

a demo on regex101.com

+1

Works lke a char,谢谢! – Ras

+1

'const string ptnBodytext = @“

(。+?)

”;'并且在构建正则表达式对象时传递'RegexOptions.Singleline'标志。 –

只需卸下\s*

const string ptnBodytext = @"<p>(.+?)</p>"; 
+2

[** Not ** true](https://regex101.com/r/yG5hW3/1)没有['DOTALL'](https://regex101.com/r/yG5hW3/2)模式。另外,'\ s *'匹配零个或多个空格字符。 – Jan