Visual Studio (2013+) 的正则表达式匹配并替换完全相同的字符串
Regexp for Visual Studio (2013+) to match and replace the exact same string
我想搜索诸如
..., "String", "String", ...
并将其替换为 say
..., "String", SAMESTRING, ...
所以主要问题是如何确保第二个字符串是第一个字符串的副本。有什么想法吗?
这是一种匹配和替换所有重复的相同字符串的方法:
using System;
using System.Text.RegularExpressions;
namespace myapp
{
class Program
{
static void Main(string[] args)
{
string input = "\"a\", \"a\", \"b\", \"c\", \"c\", \"c\", \"d\"";
string output = Regex.Replace(input, "(\"[^\"]*\")(?<=\1, \1)", "\"SAMESTRING\"");
Console.WriteLine(output);
}
}
}
输入:"a", "a", "b", "c", "c", "c", "d"
输出:"a", "SAMESTRING", "b", "c", "SAMESTRING", "SAMESTRING", "d"
它匹配双引号中的字符串,检查该字符串前面是否有相同的字符串和逗号,如果是,则将重复的字符串替换为 "SAMESTRING"。
我想搜索诸如
..., "String", "String", ...
并将其替换为 say
..., "String", SAMESTRING, ...
所以主要问题是如何确保第二个字符串是第一个字符串的副本。有什么想法吗?
这是一种匹配和替换所有重复的相同字符串的方法:
using System;
using System.Text.RegularExpressions;
namespace myapp
{
class Program
{
static void Main(string[] args)
{
string input = "\"a\", \"a\", \"b\", \"c\", \"c\", \"c\", \"d\"";
string output = Regex.Replace(input, "(\"[^\"]*\")(?<=\1, \1)", "\"SAMESTRING\"");
Console.WriteLine(output);
}
}
}
输入:"a", "a", "b", "c", "c", "c", "d"
输出:"a", "SAMESTRING", "b", "c", "SAMESTRING", "SAMESTRING", "d"
它匹配双引号中的字符串,检查该字符串前面是否有相同的字符串和逗号,如果是,则将重复的字符串替换为 "SAMESTRING"。