Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive JFlex regex

Tags:

regex

jflex

How do you achieve case insensitive regular expression patterns in JFlex.

Using the standard (?i:) notation does not seem to work.

For example:

To match the case insensitive word "class", the regex would be (?i:class).
But this doesn't work in a JFlex specification.
like image 343
ChrisM Avatar asked Oct 18 '12 01:10

ChrisM


1 Answers

Use one of the directives:

  • %caseless
  • %ignorecase

From the manual:

This option causes JFlex to handle all characters and strings in the specification as if they were specified in both uppercase and lowercase form. This enables an easy way to specify a scanner for a language with case insensitive keywords. The string "break" in a specification is for instance handled like the expression ([bB][rR][eE][aA][kK]). The %caseless option does not change the matched text and does not effect character classes. So [a] still only matches the character a and not A, too. Which letters are uppercase and which lowercase letters, is defined by the Unicode standard and determined by JFlex with the Java methods Character.toUpperCase and Character.toLowerCase. In JLex compatibility mode (-jlex switch on the command line), %caseless and %ignorecase also affect character classes.

like image 160
Bart Kiers Avatar answered Sep 23 '22 07:09

Bart Kiers