gpt4 book ai didi

regex - 正则表达式 "all three in any order and nothing more"

转载 作者:行者123 更新时间:2023-12-01 09:56:14 24 4
gpt4 key购买 nike

我需要制作一个符合以下条件的正则表达式:

1) 只有三个单词才算数学2)不分开或用分号(;)隔开3)以任何顺序4) 必须包含所有的单词,否则匹配不上

我试过这个:

^(?=(.*;|)one)(?=(.*;|)two)(?=(.*;|)three).*$

但它以某种方式计算变体,例如 oneasfafasfsaf;two;three这是错误的

请帮忙!

附言有时需要三个以上,但我想了解核心

最佳答案

你可以 capture所有 3 并检查每个是否存在,然后,如果字符串仅由 3 个单词组成:

^(?=.*?(one))(?=.*?(two))(?=.*?(three))(?:(?:\1|\2|\3);?){3}$

参见 test at regex101.com (右边的解释); Regex FAQ

关于regex - 正则表达式 "all three in any order and nothing more",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26238467/

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