function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
parkerAPTparkerAPT 

Case Insensitive Pattern Matcher

Is there a way to make a Pattern case insensitive, like in Java and most other regular expression libraries?

In Java, you can write something like:
Pattern p = Pattern.compile("foobar",Pattern.CASE_INSENSITIVE);

This will match "fOObar" or "Foobar" or any other permutation.

In Apex, it seems like you can only specify the regular expression.

- One solution would be to make the regular expression lowercase and make all strings you match against lowercase. However, if I want to get a case sensitive group, I could not do this elegantly (but can be done).

Thanks
Scott.MScott.M

There is! See this post:

 

http://community.salesforce.com/t5/Apex-Code-Development/Case-insensitive-regular-expressions/m-p/151732/highlight/false#M21444

 

in your case you would write your expression like this

 

Pattern p = Pattern.compile('(?i)foobar');