using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"<(a|h[1-6])[^>]*>(?:[a-zA-Z0-9\s\'\-\.,]|(?:<(.*)>.*<\/\1>))*<\/(a|h[1-6])>(*SKIP)(*FAIL)|\b(Find Me)\b(?=[^>]*(?:<|$))";
string input = @"Lorem ipsum find me and skip when
<b>find me</b> is inside a link tag
like this find me
<h1>find me in the title</h1>
<a href=""#"">here you can find me too</a>.
<h2 class=""heading"">Lorem Ipsum dolor find me here too</h2>
<table>
<tr><td>Cell 1 a</td><td>Find me</td></tr>
<tr><td>Cell 2 with find me</td><td><a href=""#foo"">Find me</a></td></tr>
</table>
<h3 class=""heading"">Duomo Di find me San Martino</h3>
<p>FIND ME as well</p>";
RegexOptions options = RegexOptions.IgnoreCase;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx