开发者

C# | Regex Dotall and Result Match

To the point, how i can enable dotall flags on C# Regex ? And i have this regex.

string reg = "<table>.*</table>";

And for example, i regexed th开发者_运维知识库is html text

<table id="table1">
<table id="table2">
</table>
</table>

Where does my regex stop the match ? The first </table> or the second </table>

Thanks guys.. :)


Regular expressions are greedy by default, gobbling up as much as possible. Therefore, it'll stop at the second table.

You can change this by applying the question mark modifier.

<table>.*?</table>

That said, you'll need to make sure that your regex is set up to cover multiple lines of text.


* is a 'greedy' operator - i.e. it eats up as much as possible, so it will match between the first <table> and the second </table> (providing the regular expression is configured to match over multiple lines). You can cause it to be 'non-greedy' by using *? instead.


Dotall is a regexflag so you can use like: Regex.Replace(input, regex, replace, RegexOptions.Singleline | RegexOptions.IgnoreCase)

Dotall = RegexOptions.Singleline , since it treat the string as a single line.

you can also modify regex flag in the middle of the regex instruction, like in: (?s) - Match the remainder of the pattern with the following effective flags: migs (Multiline, ignorecase, global, and singleline)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜