gpt4 book ai didi

regex - 有没有比我的正则表达式更好的解决方案

转载 作者:行者123 更新时间:2023-12-02 01:08:15 25 4
gpt4 key购买 nike

我花了一些时间试图找出关于 this website 的问题我最终找到了一个正则表达式,可以满足我被要求做的事情

练习是:

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

关于regex - 有没有比我的正则表达式更好的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com