我试着做一个能满足这个需要的正则表达式:
"a" < Match group 1
"b" < Match group 3
"a-b" < Match group 1, 2 and 3
"-" < No match
"ab" < No match我试着做一些类似(a?)(-b?)的东西,但很明显,这并不像我想要的那样。
编辑:
用一个真实的例子来更好地解释:
我尝试使用的Regex:/remind (me|him)? about (this|that)?/gm
Text | Should match?
"remind me" | Yes
"remind me about this" | Yes
"remind me about that" | Yes
"remind me about error" | No
"remind him about this" | Yes
"remind about" | NO
"remind this" | Yes
"remind error" | No
"remind me" | Yes编辑解释原因:
我需要这个正则表达式来拆分字段中的数据,比如“谁会被提醒?”“提醒短信是什么?”
remind me about this
人:我
事情:这个
remind me
人:我
事情:失踪
remind that
人物:missing
那件事:
remind me this
错误
remind about this
错误
发布于 2019-11-05 21:09:40
不完全确定,但也许这有帮助。至少它满足了您的要求:匹配"a“、"b”、“ab”,而不匹配ab和ba。
((a)-(b))|(?:\b(a)(?:[^b]|\b))|(?:(?:[^a]|\b))(b)\b发布于 2019-11-05 22:39:53
一种系统的方法是对句子结构采用分词重设。
每个分支包含一组不同的元素。
这使用分支重置。人在第一组,事物在第二组。
如果两者都不见了,那就意味着它不在那里。
remind[ ](?|(him)[ ]about[ ](this)|(me)(?:[ ]about[ ](th(?:at|is)))?|()(th(?:at|is)))
https://regex101.com/r/DPfvs0/1
如果没有可用的分支重置,则捕获组可以配对为
2的增量,即人/物。
1和2
3和4
5和6
看看哪一对匹配就行了。
remind[ ](?:(him)[ ]about[ ](this)|(me)(?:[ ]about[ ](th(?:at|is)))?|()(th(?:at|is)))
https://stackoverflow.com/questions/58719277
复制相似问题