using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(?!##?\s)([^\/\|\@\""!]*?)(##|#[@?$%]{1,3}#|\$@?\$)";
string input = @"## comment
# #comment
! Basic rules
##div
#@#div
##.ads
#@#.ads
###ads
#@##ads
example.com,~example.org##div
example.com,~example.org#@#div
example.com,~example.org##.ads
example.com,~example.org#@#.ads
example.com,~example.org###ads
example.com,~example.org#@##ads
! HTML filtering rules
! https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#html-filters
example.com##^.badstuff
example.com##^script:has-text(7c9e3a5d51cdacfc)
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#html-filtering-rules
example.org$$script[data-src=""banner""]
example.org$@$script[data-src=""banner""]
! CSS rules
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#cosmetic-css-rules
example.com#$#div { visibility: hidden; }
example.com#@$#div { visibility: hidden; }
example.com#$#.textad { visibility: hidden; }
example.com#@$#.textad { visibility: hidden; }
example.com#$##textad { visibility: hidden; }
example.com#@$##textad { visibility: hidden; }
! Extended CSS selectors
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#extended-css-selectors
example.com#?#div:has(> a[target=""_blank""][rel=""nofollow""])
example.com#@?#div:has(> a[target=""_blank""][rel=""nofollow""])
example.com#?#.banner:matches-css(width: 360px)
example.com#@?#.banner:matches-css(width: 360px)
example.com#?##banner:matches-css(width: 360px)
example.com#@?##banner:matches-css(width: 360px)
example.com#$?#div:has(> span) { display: none !important; }
example.com#@$?#div:has(> span) { display: none !important; }
example.com#$?#.banner:has(> span) { display: none !important; }
example.com#@$?#.banner:has(> span) { display: none !important; }
example.com#$?##banner:has(> span) { display: none !important; }
example.com#@$?##banner:has(> span) { display: none !important; }
! JavaScript rules
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#javascript-rules
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#scriptlets
example.com#%#window.__gaq = undefined;
example.com#@%#window.__gaq = undefined;
! Scriptlet rules
example.com##+js(aopw, Fingerprint2)
example.com#@#+js(aopw, Fingerprint2)
example.org,example.com#%#//scriptlet(""abort-on-property-read"", ""alert"")
example.com#@%#//scriptlet(""abort-on-property-read"")";
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