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:
The most common problem is to get the structure out of deeply nested regular expressions.  
Using the online service 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:

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: