我已经花了一些时间试图找出这个网站上的问题,我最终发现了一个正如我所要求的那样有效的正则表达式
练习是:
Enter a regexp that matches all the items in the first column (positive examples) but none of those in the second (negative examples). When you press "submit", you will see what matched. Positive Negative pit pt spot Pot spate peat slap two part respite
我找到的答案是
^\w?.*p[a-z ?]t\w?.*$
我发现的是所有积极的话语都有某种形式p_t
.
我匹配,p[a-z ?]t
然后^\w?.*
匹配任何前面的字母,p
并\w?.*$
匹配任何字母数字字符后t
我想知道的是有更好的方法来回答这个问题吗?
这是一个更符合这些标准的简单:
.*p.t.*
这匹配任何包含p
后跟恰好一个字符的字符序列,后跟一个t
.只要在区分大小写模式下运行,就不会匹配Pot
.
当然,如果你不需要匹配整个字符串(只是它的某些部分),这甚至更简单:
p.t