我正在使用 Java 和正则表达式,需要将一些数据拆分为多个实体。在我的输入中,单引号字符 (') 指定实体的结尾,除非它前面有转义字符,即问号 (?)。
我的 RegEx 是(?<!\\?)\\'
并且我正在使用 Scanner 将输入拆分为单独的实体。所以以下情况可以正常工作:
Hello'There becomes 2 entities: Hello and There
Hello?'There remains 1 entity: Hello?'There
但是,当我遇到想要转义问号的情况时,它不起作用。所以:
Hello??'There should become 2 entities: Hello?? and There
Hello???'There should become 1 entity: Hello???'There
Hello????'There should become 2 entities: Hello???? and There
Hello?????'There should become 1 entity: Hello????'There
Hello?????There should become 1 entity: Hello????There
Hello??????There should become 1 entity: Hello?????There
因此,规则是如果有偶数个问号,后跟引号,则应将其拆分。如果有奇数个问号,则不应拆分。
有人可以帮助修复我的正则表达式(希望有解释!)以应对多种情况吗?
谢谢,
菲尔