且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用正则表达式匹配两个字符串之间的字符串,同时排除字符串

更新时间:2022-11-11 23:35:52

将第一个和最后一个(但不是中间).*? 替换为 (?:(?!unwanted).)*?.(其中 (?:...) 是一个非捕获组,而 (?!...) 是一个负面的前瞻.)

Replace the first and last (but not the middle) .*? with (?:(?!unwanted).)*?. (Where (?:...) is a non-capturing group, and (?!...) is a negative lookahead.)

然而,这很快就会在任何实际(而不是示例)使用中以极端情况和警告告终,并且如果您询问您真正在做什么(使用真实示例,即使它们是简化的,而不是示例),您可能会得到更好的答案.

However, this quickly ends up with corner cases and caveats in any real (instead of example) use, and if you would ask about what you're really doing (with real examples, even if they're simplified, instead of made up examples), you'll likely get better answers.