logo

MMBase tips

Regular expressions

tip #4 / Mon 16 Nov 2009 / André / 0 comments

There are several tags in the mmbase taglib in which you can use regular expressions. Really helpful to filter for unwanted html-tags, like for example whether a richtext editor has included paragraph tags in your text.

Regular expressions can be used in <mm:compare /> and <mm:escaper />. This example checks for a paragraph tag at the beginning. When it finds one it does nothing. When it does not it adds one at the end and the beginning: the second <mm:compare /> inverses the pattern with the attribute 'inverse="true"'.

<mm:node number="a.news.article">
  <mm:field name="body">
    <mm:compare regexp="^<p>.*"><mm:write /></mm:compare>
    <mm:compare regexp="^<p>.*" inverse="true">
<p><mm:write /></p>
</mm:compare> </mm:field> </mm:node>

The regular expression syntax used in the mmbase taglib is the one included in Java and is very simmular to regex in Perl.

The <mm:escaper /> tag can combine several regular expression patterns after another. The taglib documentation shows an example.

0 comments

RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase API
MMBase SVN
Bugtracker
MMBase Taglib reference
The reference for all tags mm