Sunday, January 06, 2013

Regular Expressions: Still Two Problems?



You sometimes stumble upon regular expressions which are difficult to understand, e.g. what exactly means:
?:(?!(foo1|foo2)).)*
The most common problem is to get the structure out of deeply nested regular expressions.  
Using the online service http://www.regexper.com/ makes it much easier to see the structure:













Now you see that this regular expression matches all content beside "foo1" or "foo2" using the pattern explained here: http://trent-intovalue.blogspot.de/2010/01/negating-regexes.html

Using the example taken from Regular Expressions: Now You Have Two Problems:

<\/?p>|<br\s?\/?>|<\/?b>|<\/?strong>|<\/?i>|<\/?em>|   <\/?s>|<\/?strike>|<\/?blockquote>|<\/?sub>|<\/?super>|   <\/?h(1|2|3)>|<\/?pre>|<hr\s?\/?>|<\/?code>|<\/?ul>|   <\/?ol>|<\/?li>|<\/a>|<a[^>]+>|<img[^>]+\/?>

Try it out - you probably see what the author try to achieve.
Note: you have to use regular expressions in JavaScript style to use the service.

No comments: