How to match a string, but case-insensitively?

前端 未结 5 929
渐次进展
渐次进展 2020-12-29 05:59

Let\'s say that I want to match \"beer\", but don\'t care about case sensitivity.

Currently I am defining a token to be (\'b\'|\'B\' \'e\'|\'E\' \'e\'|\'E\' \'r\'|

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 06:49

    New documentation page has appeared in ANTLR GitHub repo: Case-Insensitive Lexing. You can use two approaches:

    1. The one described in @javadba's answer
    2. Or add a character stream to your code, which will transform an input stream to lower or upper case. Examples for the main languages you can find on the same doc page.

    My opinion, it's better to use the first approach and have the grammar which describes all the rules. But if you use well-known grammar, for example from Grammars written for ANTLR v4, then second approach may be more appropriate.

提交回复
热议问题