Ditty
Ditty

Reputation: 529

Regex Help. What am I doing wrong

Can somebody please help me and let me know what am I doing wrong? I am writing in code behind in C#. I am trying to find if my multiline textbox value contains HREF or href or <a href or </a> or <A HREF.

This is what I tried with Regex. But it gives me the parsing error saying Too many... Please help. Thanks

Regex strMatch = new Regex(@"^(HREF|href|<a href|</a>))", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);

Upvotes: 0

Views: 69

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

Your regex is anchored at the start of the string, so it would only match if your string started with one of the four options.

Additionally, you have an extra ) that's resulting in an invalid regex syntax.

Try this instead:

Regex strMatch = new Regex(@"(?:<a )?href|</a>"
    ,RegexOptions.Compiled|RegexOptions.IgnoreCase);

This will match your four cases, since the first two are identical (thanks to IgnoreCase), and the first is a substring of the third.

Upvotes: 1

Related Questions