Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Regex fails to match in code, works in every testing harness

Tags:

c#

.net

regex

This one is a real head scratcher for me...

var matches = Regex.Matches("<p>test something<script language=\"javascript\">alert('hello');</script> and here's <b>bold</b> and <i>italic</i> and <a href=\"http://popw.com/\">link</a>.</p>", "</?(?!p|a|b|i)\b[^>]*>");

The Regex is supposed to capture any HTML tag (open or close) that's not p, a, b, or i. I've plugged the input string and regex into countless testing pages, and every one of them return the script tag (open and close) as matches. But it absolutely doesn't work in the code. The matches variable has a count of 0.

Am I missing something incredibly obvious?

like image 436
Jeff Putz Avatar asked Feb 28 '23 18:02

Jeff Putz


1 Answers

You forgot to escape the backslash in the pattern string.

"</?(?!p|a|b|i)\\b[^>]*>"
like image 105
Guffa Avatar answered Apr 07 '23 20:04

Guffa