2 matches 2: "(Abc.-6ren">
gpt4 book ai didi

regex - 正则表达式,匹配 "UnQualified"个字符串

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

测试数据

1: "Abc.TestCase For TestCase By Abc.TestCase Using TestCase"           --> 2 matches 
2: "(Abc.TestCase For TestCase By Abc.TestCase Using TestCase)" --> 2 matches
3: "(TestCase For TestCase By Abc.TestCase Using TestCase)" --> 3 matches
4: "(Abc.TestCase For TestCase By Abc.TestCase Using Xyz.TestCase.Sub)" --> 1 match
5: "(Abc.TestCase For TestCase By Abc.TestCase Using Xyz.TestCase1)" --> 1 match

目标是获取不合格“TestCase”

已尝试以下

[^.]\bTestCase\b[^.]

虽然这行得通,但在第 2 和第 3 种情况下失败,它返回“(TestCase”“TestCase)”作为匹配项,这将在替换中导致错误结果。

为此束手无策!

在此希望得到一些帮助。

最佳答案

我想这就是您要找的:

(?<!\.)\bTestCase\b(?!\.)

换句话说,你想匹配整个单词 TestCase (即,不在另一个单词字符之前或之后),但如果它紧跟在 . 之前或之后则不是.这是一个稍微简洁的版本:

(?<!\.\w)TestCase(?!\.\w)

您写问题的方式听起来您还想排除前面或后面有括号的匹配项,例如 (TestCaseTestCase) ,但我终于意识到你只是不想在比赛中包括 parent 。用否定环视([^.](?<!\.))替换否定字符类((?!\.]))满足该要求,因为环视不会消耗它们匹配的内容。

关于regex - 正则表达式,匹配 "UnQualified"个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10912160/

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