C# 如何执行csharp(或perl)替换字符串更改循环中每个occurrence(regex)的变量?
我想用全局替换执行一个循环 和原始主题字符串更改,并包含每个替换的先前替换 请任何人帮帮我 例如,我想使用一个链 并像编辑器一样替换C# 如何执行csharp(或perl)替换字符串更改循环中每个occurrence(regex)的变量?,c#,regex,replace,C#,Regex,Replace,我想用全局替换执行一个循环 和原始主题字符串更改,并包含每个替换的先前替换 请任何人帮帮我 例如,我想使用一个链 并像编辑器一样替换 text match1 text match2 代替 text mathreplace1 "starting after first ocurrence " text match2 并在第一次出现后开始,不断改变字符串 所以如果我想要一个假设的案例,用链子代替后面的一个了望台 没有任何问题 在文本编辑器中,每次替换都保存在操作链中 总之 replace并
text match1 text match2
代替
text mathreplace1 "starting after first ocurrence " text match2
并在第一次出现后开始,不断改变字符串
所以如果我想要一个假设的案例,用链子代替后面的一个了望台
没有任何问题
在文本编辑器中,每次替换都保存在操作链中
总之
replace并继续操作的主题字符串中存储的第二个replace
直到最后一次更换
我希望你能理解我
我没有定义代码,只是希望实现一个循环,使变量成为每次替换中的输入字符串;因此,我可以使用regex命令操作替换,但每次替换都使用具有相同命令的变量字符串
==================================================
我的意思是,在操作中,每次应用命令时,正则表达式
使用输入字符串
如果替换第一个匹配项,则使用链输入
如果我第二次替换引用,它将用于输入字符串和匹配结束后的位置
我希望
它被替换为第一个字符串,然后
此操作的结果将用作新链
继续在比赛发生后的位置上
因此,我制作了一段代码片段:
我认为下面的代码是最简单的方法:
using System;
using System.Text.RegularExpressions;
using System.Text;
namespace Rextester
{
public class Program
{
public static string replaceAsWithWhatever(string inputText)
{
StringBuilder sb = new StringBuilder();
Regex rx = new Regex("as");
int prior = 0;
foreach (Match match in rx.Matches(inputText))
{
int i = match.Index;
// copy the text not matched in between
sb.Append(inputText.Substring(prior, i - prior));
// [optional] prepend some text before match
sb.Append(" [before replacement] ");
// replace match with something, possibly permutation of match
sb.Append(" [replacement of " + match.ToString() + "] ");
// append text after match
sb.Append(" [after replacement] ");
prior = i + match.Length;
}
// copy remaining string after all matches to stringbuilder
sb.Append(inputText.Substring(prior, inputText.Length - prior));
return sb.ToString();
}
public static void Main(string[] args)
{
// test cases
Console.WriteLine(replaceAsWithWhatever("text before as between as whatever"));
Console.WriteLine(replaceAsWithWhatever("sentence without matches"));
Console.WriteLine(replaceAsWithWhatever("as"));
Console.WriteLine(replaceAsWithWhatever("as ass ;)"));
}
}
}
在我之前的回答中,我假设您希望能够在循环匹配时更改替换。然而,在重读之后,我想我理解了被问到的问题 我的解释是: 就像在C预处理器中一样,定义要展开并再次匹配的 示例:将
/word/
替换为字符串中的“echo wo”
:
“wordrd”->“echo word”->“echo echo rd”
这里有一个严重的问题,如果替换项也包含一个匹配项,那么您将看到一个无限的单词
示例:将/ass/
替换为“刺客”
“屁股”->“刺客”->“刺客”->等
为了避免无限循环/字,此解决方案仅检查替换中最后一个匹配的索引,并使用前一个匹配+此偏移量作为最小索引。基本上:
示例:将/ass/
替换为字符串中的“assass”
“阿萨斯”->“刺客”->“刺客”
示例将/as/
替换为“ba”
“低音”->“低音”->“bbas”->“bbba”
代码在这里:和下面
using System;
using System.Text.RegularExpressions;
using System.Text;
/* Basically a context-free replacement, minus the infinite words */
namespace Rextester
{
public class Program
{
public static string replace(string inputText, Regex find, string replacement)
{
// to avoid a situation like the following: replace ass with assassin in string ass
// "ass" -> "assassin" -> "assassinassin" -> etc.
// it is only allowed to match after the last match in the replacement
// aka. if we were to replace "ass" with "assassin a" in string "assss" we'd get
// "ass" -> "assassin ass" -> "assassin aassassin a"
int countToSkipInReplacement = 0;
int minimumIndex = 0;
Match m;
// first check if the replacement contains matches as well
m = find.Match(replacement);
if (m.Success)
{
while (m.Success)
{
countToSkipInReplacement = m.Index + 1;
m = m.NextMatch();
}
}
while(true)
{
// uncomment to see all forms in between
//Console.WriteLine(inputText);
// find a match
m = find.Match(inputText);
// skip until the minimum index is found
while (m.Success && m.Index < minimumIndex)
m = m.NextMatch();
// if it has no further matches, return current string
if (!m.Success)
return inputText;
// glue a new inputText together with the contents before, the replacements and whatever comes after
inputText = inputText.Substring(0, m.Index) +
replacement +
inputText.Substring(m.Index + m.Length, inputText.Length - (m.Index + m.Length));
// make sure it doesn't go back and replace the text it just inserted again.
minimumIndex = m.Index + countToSkipInReplacement;
}
}
public static void Main(string[] args)
{
Regex rx = new Regex("as");
Console.WriteLine(replace("text before ass between as whatever", rx, "a"));
Console.WriteLine(replace("sentence without matches", rx, "a"));
Console.WriteLine(replace("as", rx, "a"));
Console.WriteLine(replace("as", rx, "as"));
Console.WriteLine(replace("assss", rx, "ba"));
}
}
}
使用系统;
使用System.Text.RegularExpressions;
使用系统文本;
/*基本上是上下文无关的替换,减去无限的单词*/
名称空间测试仪
{
公共课程
{
公共静态字符串替换(字符串输入文本、正则表达式查找、字符串替换)
{
//为了避免出现如下情况:用刺客代替驴子
//“驴子”->“刺客”->“刺客”->等等。
//仅允许在替换中的最后一次匹配之后进行匹配
//如果我们用“asss”串中的“刺客a”替换“asss”,我们会得到
//“驴子”->“刺客驴子”->“刺客a”
int counttoskipin替换=0;
int-minimumIndex=0;
匹配m;
//首先检查替换项是否也包含匹配项
m=查找匹配(替换);
如果(m.成功)
{
while(m.Success)
{
CountToSkipin替换=m.索引+1;
m=m.NextMatch();
}
}
while(true)
{
//取消注释以查看中间的所有表单
//控制台写入线(输入文本);
//找到匹配的
m=查找匹配(inputText);
//跳过,直到找到最小索引
而(m.Success和m.Index
如果你想,是什么阻止了你?更多信息?做点努力……举个例子会有帮助的。^^@jhonny625:这是你以前问题的翻版,我觉得不清楚。是什么让问题变得清晰:1)输入(示例字符串、已知参数、您计划如何使用它们以及在何处)、2)输出、3)您编写的代码示例、4)代码错误的描述。请注意,您可以替换正则表达式