C# 正则表达式来匹配模式,只要前面没有其他模式
我需要一个用于文本替换的正则表达式。示例:要匹配的文本是C# 正则表达式来匹配模式,只要前面没有其他模式,c#,.net,regex,C#,.net,Regex,我需要一个用于文本替换的正则表达式。示例:要匹配的文本是ABC(可以用方括号括起来),替换文本是DEF。这已经足够基本了。复杂的是,当前面有模式\[[\d]+\]\.时,我不想匹配ABC文本。-换句话说,当前面有一个单词或括号中的一组单词,后跟一个句点时 下面是要匹配的源文本的一些示例,以及正则表达式替换后的结果: 1. [xxx xxx].[ABC] > [xxx xxx].[ABC] (does not match - first part fits the pattern) 2. [
ABC
(可以用方括号括起来),替换文本是DEF
。这已经足够基本了。复杂的是,当前面有模式\[[\d]+\]\.
时,我不想匹配ABC
文本。-换句话说,当前面有一个单词或括号中的一组单词,后跟一个句点时
下面是要匹配的源文本的一些示例,以及正则表达式替换后的结果:
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#regex的味道)你想在表达式后面看到一个负面的表情。这些看起来像
(?),因此:
它匹配和不匹配的示例:
[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
[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)
正如第二种模式所预期的那样,试图使一个开放的括号出现[
强制一个匹配的封闭括号]
,是比较棘手的,但这似乎是可行的:
(?:(?<!\[[\d ]+\]\.\[)ABC\]|(?<!\[[\d ]+\]\.)(?<!\[)ABC(?!\]))
示例是使用以下代码生成的:
// 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);
}
}
}
//编译并运行:mcs so_regex.cs&&mono so_regex.exe
使用制度;
使用System.Text.RegularExpressions;
公共类SORegex{
公共静态void Main(){
字符串[]值={“[123].[ABC]”、“[123 456].[ABC]”、“[123.ABC”、“ABC”、“[ABC]”、“[ABC[”};
字符串[]应为{“失败”、“失败”、“匹配”、“匹配”、“匹配”、“失败”};
字符串模式=@“(?那些字符类看起来不太正确。什么是未缩放的右括号?这不是变长的lookbehind吗?@tchrist:.NET regex风格确实支持无限制的变长lookbehind。至于未缩放的方括号,他似乎从OP的regex复制了错误;这种情况经常发生。这不是quite工作。当第一个或第二个(?:中包含?标记时,它仍然与源[123].[ABC]中的ABC匹配。如果我从第一个标记中删除?标记,则它工作正常(并强制将括号括在ABC周围).我喜欢第二个的想法-不允许使用方括号,也不允许使用开+闭方括号,但是看起来它仍然需要一些调整。在这里测试:。(?@Yaakov修复了这两个表达式。我在你的第二个中更改了DEF
“字符串到ABC
,表示没有发生替换。这就是你的意思,不是吗?是的。只是确保你注意了。
(?:(?<!\[[\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);
}
}
}