سؤال

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.
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top