【发布时间】:2015-12-31 08:19:03
【问题描述】:
ACTIVE_LIST = ACTOR | ACTIVE_LIST and ACTOR
ACTOR = NOUN | ARTICLE NOUN
ARTICLE = a | the
NOUN = tom | jerry | goofy | mickey | jimmy | dog | cat | mouse
通过应用上述规则,我可以生成
a tom
tom and a jerry
the tom and a jerry
the tom and a jerry and tom and dog
但不是
Tom
the Tom and me
我可以只使用 python re 模块检查句子是否正确吗?我知道如何通过 [abc] 匹配某些字符,但不知道单词。 其实我正在尝试解决这个ACM problem。如果有人帮助我部分,我可以做剩下的。 这是我在这个舞台上的第一个问题。任何建议或改进都非常感谢。
【问题讨论】:
-
查看文档docs.python.org/2/library/re.html,您可以使用 re.IGNORECASE。和 [A-Z] 将匹配大小写。
-
这不是用正则表达式来描述的。这是一个解析问题,像the pyparsing module at PyPI 这样的工具会让你按照作者认为你应该看到的那样看待这个问题。
标签: python regex compiler-construction