Post Icon

Select ‘Not group’ regular expression

By joost on January 4th, 2007

I started this year with a nice regular expression fight.
An expression which I used in an application to select all files which name starts with ‘IWu’ but not ends with ‘Bean’,'Local’ or ‘Home’ followed by ‘.java’ aparently did not work properly.
The expression which I used was:

.*\\IWu.*[^(Bean)|(Home)|(Local)]\.java

Somehow with this expression, filenames like ‘….\IwuRelation.java’ did not validate as true. A small test showed that the group ‘Bean’ in the expression was the cause. Why I don’t know. Maybe because ‘Bean’ and ‘Relation’ share 3 letters; ‘a’,'e’,'n’.

So aparently grouping a combination of letters this way, does not work. What than? It’s easy to create an expression using a group, but how to create one which validates ‘not group’.
After trying many different expressions, I finally found one which seems to do exactly what I wanted it to do. All tests I created were successful for this expression.

.*\\IWu.*(?<!Bean|Home|Local)\.java

This expression uses a negative-lookbehind (”?<!”) function of regular expressions. It looks back and makes sure it does not contain ‘Bean’,'Home’ or ‘Local’. If it does, validation fails.
If you need to define this expression in an XML file, like I did, you have to use CDATA because a ‘<’ is used in the expression.

<![CDATA[.*\\IWu.*(?<!Bean|Home|Local)\.java]]>

RSS feed

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.

*
To prove that you're not a bot, enter this code
Anti-Spam Image