gpt4 book ai didi

c# - 拿走一切,除非它看到 foo

转载 作者:行者123 更新时间:2023-11-28 15:35:38 28 4
gpt4 key购买 nike

学习正则表达式。

我想匹配所有内容,除非它看到 foo

输入:

take everything 1 foo take everything 2 foo take everything 3
take everything 4

预期:

match 1 : `take everything 1 `
match 2 : ` take everything 2 `
match 3 : ` take everything 3 `
match 4 : `take everything 4`

尝试:

  1. ([^foo]*) http://regex101.com/r/rT0wU0/1

    结果:

    匹配 1:拿走所有 1

    匹配2-4、6-8、10:

    匹配 5:拿走所有 2

    匹配9:拿走所有3拿走所有4

  2. (.*(?!foo)) http://regex101.com/r/hL4gP7/1

    结果:

    匹配1:拿走一切1 foo 拿走一切2 foo 拿走一切3

    匹配2、3:

    匹配4:拿走所有4

请赐教。

最佳答案

将字边界 \b 与负向前瞻结合使用。

\b(?:(?!foo).)+

示例:

String s = @"take everything 1 foo take everything 2 foo take everything 3
take everything 4";

foreach (Match m in Regex.Matches(s, @"\b(?:(?!foo).)+"))
Console.WriteLine(m.Value.Trim());

输出

take everything 1
take everything 2
take everything 3
take everything 4

关于c# - 拿走一切,除非它看到 foo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25702805/

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