1

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 this 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.. :)

3 Answers3

3

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.

Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42
2

* 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.

mdm
  • 12,480
  • 5
  • 34
  • 53
0

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)

Eduardo Aguiar
  • 247
  • 2
  • 3