Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Match a character which is not followed by another specific character

I'm writing a CodeMirror extension for Brackets. To defineSimpleCodeMode I need to do some pattern matching and I'm trying to figure out how to achieve $subject.

e.g.

Match < of all the html tags

<body>

And ignore html tags which are followed by <%

<% if %> 

Note: I only want to get the starting < of it

If some can help me out it would be a great help. Please do let me know if you need anymore details.

Thanks!

like image 975
Jerad Rutnam Avatar asked Aug 10 '16 10:08

Jerad Rutnam


1 Answers

While this seems to be a bad idea, I can see two ways of doing it :

1. Searching for < followed by anything but the % character, then ignoring it

(<)(?:[^%])

The [^] sequence allows you to search for anything but the following character.

The (?:) sequence is for non capturing groups.

2. (Better, if supported) Searching for input not followed by % with a negative lookahead

<(?!%)

The (?!) sequence succeeds if it doesn't match the following character, but is not captured.

If you also want to do it for %>, you can just "reverse" the first option :

(?:[^%])(>)

Or you need a negative lookbehind :

(careful here, the lookahead won't work as you need to go backwards)

(?<!%)>

like image 190
Azaghal Avatar answered Oct 08 '22 22:10

Azaghal