Reputation: 49
I want to count links which are having special symbol (underscore) . I have written regex its working fine in an online editor/php editor, but not working in C# code:
<
(?<Tag_Name>(a)|img)\b
[^>]*?
\b(?<URL_Type>(?(2)href|src))
\s*=\s*
(?:"(?<URL>(?:\\.|[^\\"_#?&]++)*(?:_|(?<Query>[#?&]))(?:\\.|[^"\\]++)*)"
| '(?<URL>(?:\\.|[^\\'_#?&]++)*(?:_|(?<Query>[#?&]))(?:\\.|[^'\\]++)*)')
but in C# code its giving compilation error
MatchCollection underscoreLinks = Regex.Matches(strIn, "<(?<Tag_Name>(a)|img)\b[^>]*?\b(?<URL_Type>(?(2)href|src)) \s*=\s*(?:"(?<URL>(?:\\.|[^\\"_#?&]++)*(?:_|(?<Query>[#?&]))(?:\\.|[^"\\]++)*)"| '(?<URL>(?:\\.|[^\\'_#?&]++)*(?:_|(?<Query>[#?&]))(?:\\.|[^'\\]++)*)')", RegexOptions.IgnoreCase | RegexOptions.Multiline);
Upvotes: 1
Views: 989
Reputation: 6511
There are some things you need to correct:
@"pattern"
@"the ""pattern"" with quotes"
.[^\\"_#?&]++
to (?>[^\\"_#?&]+)
.RegexOptions.IgnorePatternWhitespace
.string pattern = @"
<
(?<Tag_Name>(a)|img)\b
[^>]*?
\b(?<URL_Type>(?(2)href|src))
\s*=\s*
(?:""(?<URL>(?>\\.|[^\\""_#?&]+)*(?:_|(?<Query>[#?&]))(?>\\.|[^""\\]+)*)""
| '(?<URL>(?>\\.|[^\\'_#?&]+)*(?:_|(?<Query>[#?&]))(?>\\.|[^'\\]+)*)')
";
Regex re = new Regex( pattern,
RegexOptions.IgnoreCase | RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace);
MatchCollection underscoreLinks = re.Matches(text);
Upvotes: 3