正则表达式不符合要求
Regex not meeting the requirement
我正在尝试学习Regex
构造,但遇到一个问题。
问题陈述:
应匹配以下短语的正则表达式:
rap them
tapeth
apth
wrap/try
sap tray
87ap9th
apothecary
但不应匹配
aleht
happy them
tarpth
Apt
peth
tarreth
ddapdg
apples
shape the
我的尝试:
^[a-z0-9]+p.?t
我的正则表达式也匹配 tarpth
。我该如何解决?
看起来 this regex 成功了,这是一个非常具体的问题:
(ap)(.?)t
这符合这个要求:
^([a-z0-9]+p.{1}|[a-su-z0-9]+p)t
演示:
这应该可以做到 - 您在 p
:
之前没有要求 a
.*ap.?t.*
我正在尝试学习Regex
构造,但遇到一个问题。
问题陈述: 应匹配以下短语的正则表达式:
rap them
tapeth
apth
wrap/try
sap tray
87ap9th
apothecary
但不应匹配
aleht
happy them
tarpth
Apt
peth
tarreth
ddapdg
apples
shape the
我的尝试:
^[a-z0-9]+p.?t
我的正则表达式也匹配 tarpth
。我该如何解决?
看起来 this regex 成功了,这是一个非常具体的问题:
(ap)(.?)t
这符合这个要求:
^([a-z0-9]+p.{1}|[a-su-z0-9]+p)t
演示:
这应该可以做到 - 您在 p
:
a
.*ap.?t.*