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

Posted September 29, 2006 at 12:59 PM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 30, 2006 at 10:50 AM // reply »
15 Comments

So what exactly are the benefits of using the Java string classes? Is it just a performance issue?


Sep 30, 2006 at 11:16 AM // reply »
6,516 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.


Feb 2, 2007 at 4:09 PM // reply »
164 Comments

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.


Feb 2, 2007 at 4:15 PM // reply »
164 Comments

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.


Feb 2, 2007 at 4:23 PM // reply »
6,516 Comments

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.


Feb 3, 2007 at 12:40 AM // reply »
164 Comments

Thanks for the leads, Ben!


Feb 3, 2007 at 9:17 AM // reply »
6,516 Comments

Trust me, once you go underlying Java string methods... you never go back.


Feb 15, 2009 at 8:00 PM // reply »
1 Comments

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


Feb 15, 2009 at 8:37 PM // reply »
6,516 Comments

@Brian,

I am not sure what you are trying to do? Can you explain a bit more?


Apr 17, 2009 at 8:25 AM // reply »
1 Comments

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"?


Apr 17, 2009 at 8:27 AM // reply »
6,516 Comments

@Firoz,

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

\band\b

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


Jul 13, 2009 at 10:51 AM // reply »
1 Comments

Thanks. Very useful information.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »