Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with:

Case Insensitive Java Regular Expressions - How Did I Miss That!?!

By Ben Nadel on
Tags: ColdFusion

As you all know, in ColdFusion, when you want to do a find or replace with no case sensitivity, you just append "NoCase" to the method call. We have all used:

  • FindNoCase()
  • REFindNoCase()
  • ReplaceNoCase()
  • REReplaceNoCase()

As you well know (if you follow my blog), I am a huge fan of using the Java String methods for regular expression find and manipulation. The problem with that though is that there is no "NoCase" type methods built into the Java string (except for String::equalsIgnoreCase(), but that doesn't use regular expressions). I thought the only way to work around this was to either put all characters in regular expression (ie [a-zA-Z]), but it turns out there is a flag for performing a case-insensitive search:

(?i)

How did I miss that? I have been doing regular expressions for a long time now and this one has escaped me. Crazy! But, it's so awesome. You just put the flag (?i) in your regular expression and everything to the right of it will be case-insensitive:

  • <!--- Set string value. --->
  • <cfset strText = "Libby is HOT" />
  •  
  • <!--- Check case-sensitive match. --->
  • #strText.Matches( "[a-z ]+" )#
  •  
  • <!--- Check case-INsensitive match. --->
  • #strText.Matches( "(?i)[a-z ]+" )#

The first method outputs NO... the second method outputs YES. How freakin' cool is that. You can even perform case insensitive sub-strings:

  • <!--- Only allow case-INsensitivity on LIBBY and HOT. --->
  • #strText.Matches( "(?i:libby)[a-z ]+(?i:hot)" )#

This also outputs YES.

So anyway, that's pretty freakin' cool! I don't know how I missed that when learning Regular Expressions, but now that I know, it is sooo on.

Oh, and just be careful. This is only for JAVA regular expressions. If you try to use this type of notation in ColdFusion method calls, it will throw an error:

Malformed regular expression "(?i:libby)[a-z ]+(?i:hot)". Reason: Sequence (?:...) not recognized.

... Just another reason I am huge fan of using the underlying Java methods.



Looking For A New Job?

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments

It can be a performance issue, but it's also a utility issues. As I describe above, the regular expression methods used on the Java string class:

String::Matches()
String::ReplaceFirst()
String::ReplaceAll()

... allow for much more powerful AND faster regular expressions. Those methods can handle all the look behinds (negative and possitive) which I don't think ColdFusion can handle at all. That means that they can do much more than things like:

REFind()
REReplace()

So, it's speed, but it's also flexability.

Reply to this Comment

If (?i) and (?i:regex) works, (?-i) might also work, which with some regex libraries turns off case insensitivity for the remainder of the regular expression, e.g.: (?i)libby(?-i)[a-z]

Other things to test, which work with some libraries:

(?s) / (?-s) : Turn on/off dot matches newline.
(?m) / (?-m) : Turn on/off caret and dollar match after and before newlines.

If those work, you should also be able to do, e.g., (?i-sm) to turn on "i" and "m", but turn off "s" for the remainder of the regex.

Reply to this Comment

BTW, is there anything that needs to be done beyond what you show in your example code to make the Java methods available? Having extended regex functionality available to me in ColdFusion (particularly lookbehinds) has been a dream.

Reply to this Comment

Steve,

You know more about regular expressions than I do. I have not tried using (nor did I know about) flags that would turn off previous flags. As far as I know though, CFMX6/7 is running on top of Java 1.4.2 or something, so theoretically, anything that works in that edition of Java will run in ColdFusion (when using the underlying Java methods).

Try searching my site for ReplaceAll() and ReplaceFirst() which are the two main java RegEx string methods that I use:

http://www.bennadel.com/search/replaceall%20replacefirst

You also might want to search for Split(). Also another great Java String method:

http://www.bennadel.com/search/split(

Other than that, I am sure once you figure out the methods calls, you seem to know the nuts and bolts of the regex stuff better than I do, you will do great. You will also find that Java Regular expressions are NICE AND FAST.

Also, you might want to search for pattern / matcher:

http://www.bennadel.com/search/util.regex

Let me know if there is anything I can help with.

Reply to this Comment

I tried to implement this in the following way

if (s1.indexOf((?i)s2) >= 0)

Where s1 and s2 are string variables. Trying to get an if statement that worked if s1 contained s2 anywhere in it and then perform a loop.

I fought with it for awhile and gave up.

Any thought why this did not work (what syntax did I get wrong?).
Thanks,
bg

Reply to this Comment

This is great!!! Thanks for the tip!

Slighly off-topic, using a reg. expression and the replaceAll function, can you replace only whole words, eg. if I wanted to replace all "and" with "xyz", but only if "and" is not part of another word, as in "hand"?

Reply to this Comment

@Firoz,

Yeah, you just have to use word boundaries: \b

\band\b

... will get "and" only when its a whole word.

Reply to this Comment

Hi! It doesn't work with accents … :/
Try: Águia
I consider this a bug.

Do you know some workaround?

Reply to this Comment

class DontWorkWithAccent {

public static void main(String[] args) {
String str = "Águia";
str = str.replaceAll("(?i)[á]", "a");
System.out.println(str);
str = str.replaceAll("(?i)[Á]", "a");
System.out.println(str);
}
}

Reply to this Comment

@Leandro,

Hmmm, interesting. I know you can use hex-based values in the regular expression. Example:

[^\x7F]

... so I know it can handle any character at some level. I wonder why this is not working as a standard character class.

Reply to this Comment

Have a way to make a replaceAll in Java equivalent this maked in PHP (http://bit.ly/bZilOh):

word >a href="word"<word>/word<word word

to

repl >a href="word"<repl>/word<repl repl

note thats only "word"s outside a tag are replaced

Reply to this Comment

@Celso,

In the link you gave me, it looks like they are using a negative look-ahead to confirm that they are not inside a tag. Java definitely supports the negative look-ahead, and I pretty sure that ColdFusion also supports them (I believe it is the look-behinds that ColdFusion doesn't support).

Reply to this Comment

Very very very awesome !!!

I like it!
Seriously, I'm frustrated to don't know that rather!
I always wanted to use regex simplier in CF.
The traditional RE[..]() are too limited compared to java methods so generally I use your component, PaternMatcher.cfc (it's very useful!)!

Thanks for your tip Ben!

Reply to this Comment

Did you have make an entry that reassemble all java methods hidden in native CF objects ? As this trick up above.

Reply to this Comment

@Brian,

I know this is really (I mean really) late respons to Brian but... This page was high up on the Google search for "java matcher case insensitive" so I figure others will get here too and maybe wonder about the same thing.

String.indexOf does not use regular expressions so you can't do the magic of (?i) in a parameter to indexOf. So even if you had correct java syntax, it wouldn't work.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.