<p>I'm working on a regular expression in a <code>.NET</code> project to get a specific tag.  I would like to match the entire DIV tag and its contents:</p>

<pre><code>&lt;html&gt;
   &lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt;
   &lt;body&gt;
     &lt;p&gt;The first paragraph.&lt;/p&gt;
     &lt;div id='super_special'&gt;
        &lt;p&gt;The Store paragraph&lt;/p&gt;
     &lt;/div&gt;
     &lt;/body&gt;
  &lt;/head&gt;
</code></pre>

<p>Code:</p>

<pre><code>    Regex re = new Regex("(&lt;div id='super_special'&gt;.*?&lt;/div&gt;)", RegexOptions.Multiline);


    if (re.IsMatch(test))
        Console.WriteLine("it matches");
    else
        Console.WriteLine("no match");
</code></pre>

<p>I want to match this:</p>

<pre><code>&lt;div id="super_special"&gt;
   &lt;p&gt;Anything could go in here...doesn't matter.  Let's get it all&lt;/p&gt;
&lt;/div&gt;
</code></pre>

<p>I thought <code>.</code> was supposed to get all characters, but it seems to having trouble with the carriage returns.  What is my regex missing?</p>

<p>Thanks.</p>

[{'answer_id': 78985, 'author': 'mopoke', 'author_id': 14054, 'author_profile': 'https://Stackoverflow.com/users/14054', 'pm_score': 1, 'selected': False, 'text': '<p>Depends what language you\'re working in. \nFor example, in perl you\'d use the regex modifier s:</p>\n\n<pre><code>m{&lt;div id="super_special"&gt;.*?&lt;/span&gt;}s\n</code></pre>\n'}, {'answer_id': 78989, 'author': 'Mitchel Sellers', 'author_id': 13279, 'author_profile': 'https://Stackoverflow.com/users/13279', 'pm_score': 1, 'selected': False, 'text': "<p>What language are you using?  In .NET you must set an option to ensure that it isn't single line.  </p>\n"}, {'answer_id': 78992, 'author': 'Nescio', 'author_id': 14484, 'author_profile': 'https://Stackoverflow.com/users/14484', 'pm_score': 0, 'selected': False, 'text': '<p>. (dot) Matches any single character except line break characters \\r and \\n. Most regex flavors have an option to make the dot match line break characters too. . matches x or (almost) any other character </p>\n'}, {'answer_id': 78993, 'author': 'dimarzionist', 'author_id': 10778, 'author_profile': 'https://Stackoverflow.com/users/10778', 'pm_score': 0, 'selected': False, 'text': '<p>maybe:    .<em>[\\r\\n]</em>.<em>[\\r\\n]</em></p>\n'}, {'answer_id': 78995, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 1, 'selected': False, 'text': '<p>Depends on the language. If on python, you are missing the re.S flag, like this (to remove the match):</p>\n\n<pre><code>re.compile(\'&lt;div id="super_special"&gt;.*?&lt;/div&gt;\',re.S).sub(your_html,\'\')\n</code></pre>\n\n<p>Similar flags exist for other regexps implementations, they are called "Single Line" or "Multi Line" or something like that.</p>\n\n<p>But <strong>DO NOT USE REGEXPS TO PARSE HTML</strong>. It\'s a direct path to maintenance hell. Use a HTML parser like Beautiful Soup. Check <a href="https://stackoverflow.com/questions/55391/python-regular-expression-for-html-parsing-beautifulsoup#55424">these</a> <a href="https://stackoverflow.com/questions/2861/options-for-html-scraping#5093">links</a> for useful resources in that direction.</p>\n'}, {'answer_id': 79024, 'author': 'Jörg W Mittag', 'author_id': 2988, 'author_profile': 'https://Stackoverflow.com/users/2988', 'pm_score': 3, 'selected': False, 'text': "<p>Please, pretty please, do yourself a <em>huge</em> favor: use an HTML parser for parsing HTML. Seriously. That's what they are there for.</p>\n\n<p>HTML is a <em>very</em> complex language. No matter <em>how long</em> you will be tweaking, fiddling, fixing, honing your Regexp, there will <em>always</em> be a case you're missing.</p>\n\n<p>Anyway, you have to tell your Regexp engine to match multiple lines instead of just one. In some of the most popular ones you do that by applying the <code>/m</code> modifier.</p>\n\n<p>But let me repeat: <em>please</em> use an HTML parser. Everytime someone uses a Regexp to parse HTML, a kitten dies ...</p>\n"}, {'answer_id': 79032, 'author': 'Bennor McCarthy', 'author_id': 14451, 'author_profile': 'https://Stackoverflow.com/users/14451', 'pm_score': 1, 'selected': False, 'text': '<p>The problem is that the . metacharacter doesn\'t match newlines by default. You have to use the single-line modifier to achieve this. In .NET, you can either use RegexOptions.SingleLine as the last parameter to the method you\'re using, or use the modifier directly in the pattern, e.g:</p>\n\n<pre><code>(?s)(&lt;div id="super_special"&gt;.*?&lt;/div&gt;)\n</code></pre>\n'}, {'answer_id': 79066, 'author': 'Mike Tunnicliffe', 'author_id': 13956, 'author_profile': 'https://Stackoverflow.com/users/13956', 'pm_score': 1, 'selected': False, 'text': '<p>Most languages have some way to make . match newlines:</p>\n\n<ul>\n<li>In Java: Pattern.compile("pattern", Pattern.MULTILINE);</li>\n<li>In Perl and Ruby: /pattern/m</li>\n<li>In VB: Regex.IsMatch(s, "pattern", RegexOptions.Multiline)</li>\n</ul>\n\n<p>In general it\'s not a good idea to use regexp to match XML/HTML, because XML/HTML tags can be nested, for example:</p>\n\n<pre><code>  &lt;div id="super_special"&gt;\n     &lt;div&gt;Nothing&lt;/div&gt;\n     &lt;p&gt;Anything could go in here...doesn\'t matter.  Let\'s get it all&lt;/p&gt;\n  &lt;/div&gt;\n</code></pre>\n\n<p>... here you could easily end up matching:</p>\n\n<pre><code>  &lt;div id="super_special"&gt;\n     &lt;div&gt;Nothing&lt;/div&gt;\n</code></pre>\n\n<p>On the other hand, if you know <strong>for sure</strong> that the HTML you are matching will always be safe for your regexp, then don\'t let me stop you (although, even then you should think twice about saving your future self from a potential debugging headache).</p>\n'}, {'answer_id': 79079, 'author': 'André Chalella', 'author_id': 4850, 'author_profile': 'https://Stackoverflow.com/users/4850', 'pm_score': 2, 'selected': True, 'text': '<p>Out-of-the-box, without special modifiers, most regex implementations don\'t go beyond the end-of-line to match text. You probably should look in the documentation of the regex engine you\'re using for such modifier.</p>\n\n<p>I have one other advice: beware of greed! Traditionally, regex <strong>are</strong> greedy which means that your regex would probably match this:</p>\n\n<pre><code>&lt;div id="super_special"&gt;\n  I\'m the wanted div!\n&lt;/div&gt;\n&lt;div id="not_special"&gt;\n  I\'m not wanted, but I\'ve been caught too :(\n&lt;/div&gt;\n</code></pre>\n\n<p>You should check for a "not-greedy" modifier, so that your regex would stop matching text at the <strong>first</strong> occurence of <code>&lt;/div&gt;</code>, not at the <strong>last</strong> one.</p>\n\n<p>Also, as others have said, consider using an HTML parser instead of regexes. It will save you a lot of headache.</p>\n\n<p><em>Edit: even a non-greedy regex wouldn\'t work as expected either, if <code>&lt;div&gt;</code>s are nested! Another reason to consider using an HTML parser.</em></p>\n'}, {'answer_id': 79377, 'author': 'Mike Kantor', 'author_id': 14607, 'author_profile': 'https://Stackoverflow.com/users/14607', 'pm_score': 0, 'selected': False, 'text': '<p>None of these regex suggestions will work. Depending on whether they\'re greedy or not, they will match either the very last &lt;/div&gt; in the document, or the very first &lt;/div&gt; after your starting string, which may be a div nested inside the one you\'re interested in.</p>\n\n<p>Regular expressions are not really the ideal tool for this purpose, but if your situation is simple enough that you don\'t really want to parse the HTML, you can do this using a Microsoft-proprietary extension to regex available in .NET. For a nice explanation, see <a href="http://www.codeproject.com/KB/recipes/Nested_RegEx_explained.aspx" rel="nofollow noreferrer">this nice article by Morten Maate</a>.</p>\n'}, {'answer_id': 110757, 'author': 'Martijn', 'author_id': 17439, 'author_profile': 'https://Stackoverflow.com/users/17439', 'pm_score': 0, 'selected': False, 'text': '<p>Regular expressions alone are simply not powerful enough to solve your problem. You need something more powerful, such as context-free grammars. See <a href="http://en.wikipedia.org/wiki/Chomsky_hierarchy" rel="nofollow noreferrer">Chomsky hierarchy</a> at Wikipedia.</p>\n\n<p>In other words (as has been said before), don\'t use regex to parse HTML.</p>\n'}]
['https://Stackoverflow.com/questions/78978', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/27870/']