某些特殊字符只有在它们前面有转义字符时才允许使用

Some special characters are allowed only if they are preceded by an escape character

我想为 class 个字符串构造一个 regular expression (in the style of lex, with a more OCaml-like syntax),其中 4 个字符 []# , ' 仅当它们前面有转义字符 '.

时才允许

这里有一些有效的例子:

这里有一些无效的例子:

希望定义清楚。首先,有谁知道如何构造这样的正则表达式?二、有谁知道如何构造这样一个可以被ocamllex接受的regular expression (in the style of lex, with a more OCaml-like syntax)

除了几个例子,你没有说接受的字符串看起来像。只是为了具体起见,假设允许 lower-case 个字母和数字,并且仅当前面有 '.

时才允许使用 4 个特殊字符

然后,这由一组 36 one-character 个字符串和 4 个 two-character 个字符串的 Kleene 闭包描述。

看起来像这样:

 (['a' - 'z' '0' - '9'] | '\'' ['\'' '#' '[' ']'])*