- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要一个用于文本替换的正则表达式。示例:要匹配的文本是ABC
(可以用方括号括起来),替换文本是DEF
。这已经足够基本了。复杂的是,当 ABC
文本前面有模式 \[[\d ]+\]\.< 时,我不想匹配它
- 换句话说,当它前面是括号中的一个词或一组词,后跟一个句点。
下面是一些要匹配的源文本示例,以及进行正则表达式替换后的结果:
1. [xxx xxx].[ABC] > [xxx xxx].[ABC] (does not match - first part fits the pattern)
2. [xxx xxx].ABC > [xxx xxx].ABC (does not match - first part fits the pattern)
3. [xxx.ABC > [xxx.DEF (matches - first part has no closing bracket)
4. [ABC] > [DEF] (matches - no first part)
5. ABC > DEF (matches - no first part)
6. [xxx][ABC] > [xxx][DEF] (matches - no period in between)
7. [xxx]. [ABC] > [xxx] [DEF] (matches - space in between)
归根结底是:我如何指定前面的模式,当按照描述出现时将阻止匹配?在这种情况下,模式是什么? (正则表达式的 C# 风格)
最佳答案
你想要一个消极的后视表达。这些看起来像 (?<!pattern)
,所以:
(?<!\[[\d ]+\]\.)\[?ABC\]?
请注意,这不会强制在 ABC 周围匹配一对方括号;它只允许在前面有一个可选的左括号,在后面有一个可选的右括号。如果你想强制匹配或不匹配,你必须使用交替:
(?<!\[[\d ]+\]\.)(?:ABC|\[ABC\])
这使用非捕获括号来分隔交替。如果您想实际捕获 ABC,您可以将其变成一个捕获组。
ETA: 第一个表达式似乎失败的原因是它匹配 ABC]
,而 [
前面没有禁止文本。左括号 [
是可选的,所以它不匹配。解决这个问题的方法是将可选的开括号 [
转移到否定的后视断言中,如下所示:
(?<!\[[\d ]+\]\.\[?)ABC\]?
它匹配和不匹配的示例:
[123].[ABC]: fail (expected: fail)
[123 456].[ABC]: fail (expected: fail)
[123.ABC: match (expected: match)
matched: ABC
ABC: match (expected: match)
matched: ABC
[ABC]: match (expected: match)
matched: ABC]
[ABC[: match (expected: fail)
matched: ABC
试图使左括号 ]
强制匹配右括号 ojit_code 的存在,如第二个模式预期的那样,比较棘手,但这似乎有效:
(?:(?<!\[[\d ]+\]\.\[)ABC\]|(?<!\[[\d ]+\]\.)(?<!\[)ABC(?!\]))
它匹配和不匹配的示例:
[123].[ABC]: fail (expected: fail)
[123 456].[ABC]: fail (expected: fail)
[123.ABC: match (expected: match)
matched: ABC
ABC: match (expected: match)
matched: ABC
[ABC]: match (expected: match)
matched: ABC]
[ABC[: fail (expected: fail)
示例是使用以下代码生成的:
// Compile and run with: mcs so_regex.cs && mono so_regex.exe
using System;
using System.Text.RegularExpressions;
public class SORegex {
public static void Main() {
string[] values = {"[123].[ABC]", "[123 456].[ABC]", "[123.ABC", "ABC", "[ABC]", "[ABC["};
string[] expected = {"fail", "fail", "match", "match", "match", "fail"};
string pattern = @"(?<!\[[\d ]+\]\.\[?)ABC\]?"; // Don't force [ to match ].
//string pattern = @"(?:(?<!\[[\d ]+\]\.\[)ABC\]|(?<!\[[\d ]+\]\.)(?<!\[)ABC(?!\]))"; // Force balanced brackets.
Console.WriteLine("pattern: {0}", pattern);
int i = 0;
foreach (string text in values) {
Match m = Regex.Match(text, pattern);
bool isMatch = m.Success;
Console.WriteLine("{0}: {1} (expected: {2})", text, isMatch? "match" : "fail", expected[i++]);
if (isMatch) Console.WriteLine("\tmatched: {0}", m.Value);
}
}
}
关于c# - RegEx 匹配一个模式,只要它前面没有不同的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4081294/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!