Sweet Regex - "While Not Between"
(
Aug 17 2007 - 01:16:51 PM by
Timothy Khouri) - [
print blog
post]
I love when a complex problem is solved with a regular expression... and I probably would not have had any other way to accomplish this need without coming up with a nice regex I call "While Not Between".
Basically, the scenario was this: While writing an article on Eels, I wanted to include an HTML sample containing an "un-ordered list". This is simple enough, but the problem was that the Eels article engine was turning this:
<ul>
<li>Test</li>
</ul>
Into this:
So, after debugging for a few hours and stepping through the article engine's formatting code, I realized that I didn't want to parse a "<ul>" tag while it was between "[code][/code]" blocks. So, I came up with this one regex to rule them all:
(?<!aaa((?!bbb)[\s\S])*)SomeText
This will match the word "SomeText" in this sentense, but it won't match "aaaSomeTextbbb". I've explained it a little better on RegexLib.com.