Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regex to find specific string not in html tag

Tags:

I'm having some difficulty with a specific Regex I'm trying to use. I'm searching for every occurrence of a string (for my purposes, I'll say it's "mystring") in a document, EXCEPT where it's in a tag, e.g.

<a href="_mystring_"> 

should not match, but

<a href="someotherstring">_mystring_</a> 

Should match, since it's not inside a tag (inside meaning "inside the < and > markers") I'm using .NET's regex functions for this as well.

like image 511
Sukasa Avatar asked Jun 05 '09 20:06

Sukasa


People also ask

How do you match a specific sentence in regex?

Example is: string pattern = @"(Band) (? <Band>[A-Za-z ]+) (? <City>@[A-Za-z ]+) (?

How do I check if a string is in regular expressions?

Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and false otherwise.

What is HTML regex?

Regular expressions, or regex for short, are a series of special characters that define a search pattern. These expressions can remove lengthy validation functions and replace them with simple expressions.

How do I not match a character in regex?

There's two ways to say "don't match": character ranges, and zero-width negative lookahead/lookbehind. Also, a correction for you: * , ? and + do not actually match anything. They are repetition operators, and always follow a matching operator.


1 Answers

This should do it:

(?<!<[^>]*)_mystring_ 

It uses a negative look behind to check that the matched string does not have a < before it without a corresponding >

like image 127
Nick Higgs Avatar answered Oct 14 '22 09:10

Nick Higgs