using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?<!foo)bar(?!.*foo).*";
string input = @"bar //true
,bar, //true
,,bar,, //true
barbar //true
,bar,bar, //true
,,bar,,bar,, //true
barbarbar //true
,bar,bar,bar, //true
,,bar,,bar,,bar,, //true
foo //false
,foo, //false
,,foo,, //false
foobar //false
,foo,bar, //false
,,foo,,bar,, //false
barfoo //false
,bar,foo, //false
,,bar,,foo,, //false
foofoo //false
,foo,foo, //false
,,foo,,foo,, //false
foobarbar //false
,foo,bar,bar, //false
,,foo,,bar,,bar,, //false
barfoobar //false
,bar,foo,bar, //false
,,bar,,foo,,bar,, //false
barbarfoo //false
,bar,bar,foo, //false
,,bar,,bar,,foo,, //false
barfoofoo //false
,bar,foo,foo, //false
,,bar,,foo,,foo,, //false
foobarfoo //false
,foo,bar,foo, //false
,,foo,,bar,,foo,, //false
foofoobar //false
,foo,foo,bar, //false
,,foo,,foo,,bar,, //false
foofoofoo //false
,foo,foo,foo, //false
,,foo,,foo,,foo,, //false";
RegexOptions options = RegexOptions.Multiline;
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