When you start to do a lot of LiveText recognizers you find yourself wanting to do more advanced stuff with your regular expressions. For instance you might want to do case insensitive patterns or use some of the others regular expression modifiers. This post will show you how to do this.
By default the regular expressions you specify for your recognizers are case sensitive. This is normally fine unless you really want it to be case insensitive. Since the LiveText engine is in Java you may use the supported Java modifiers for your regular expressions. Normally the modifiers are specified when you “compile the pattern” in Java (java.util.regex.Pattern.compile(pattern, modifiers)) but as you don’t have access to this process you can’t do that.
There is however another way…
You can embed some modifiers in the pattern such as Pattern.MULTILINE, Pattern.UNICODE_CASE, Pattern.DOTALL and most of all Pattern.CASE_INSENSITIVE! You embed the modifier in the start of the pattern. So instead of doing a case insensitive pattern like this (to recognizer “lotus” and “Lotus”):
[Ll]otus
you do
(?i)lotus
Cool isn’t it?
The following modifiers are supported in Java though not all makes sense for LiveText:
- Pattern.CASE_INSENSITIVE = (?i)
- Pattern.MULTILINE = (?m)
- Pattern.DOTALL = (?s)
- Pattern.UNICODE_CASE = (?u)
Please bear in mind that it probably only makes sense to use DOTALL and CASE_INSENSITIVE.
That’s all for this post. All the posts in the series may be found under the extending_notes8 tag.