Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match the character '<' not followed by ('a' or 'em' or 'strong')?

How would I make a regular expression to match the character < not followed by (a or em or strong)

So <hello and <string would match, but <strong wouldn't.

like image 424
Kyle Avatar asked Apr 25 '10 00:04

Kyle


People also ask

How do you match a character except one?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).

How do I match a character in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .

How do you match a character in Python?

Using m option allows it to match newline as well. Matches any single character in brackets. Matches 0 or more occurrences of preceding expression. Matches 1 or more occurrence of preceding expression.


2 Answers

Try this:

<(?!a|em|strong) 
like image 81
Andrew Hare Avatar answered Sep 21 '22 13:09

Andrew Hare


You use a negative lookahead, the simplest form for which is (for this problem):

<(?!a|em|strong) 

The one issue with that is that it will ignore <applet>. A way to deal with that is by using \b, which is a zero-width expression (meaning it captures none of the input) that matches a word to non-word or non-word to word transition. Word characters are [0-9a-zA-Z_]. So:

<(?!(a|em|strong)\b) 
like image 37
cletus Avatar answered Sep 23 '22 13:09

cletus