简体   繁体   English

如何在使用Regex.Replace方法时找到子字符串?

[英]How to get found substring while using Regex.Replace method?

I'm using this code to replace all found values in string by indexes: 我正在使用此代码来替换索引中的所有找到的值:

int i = 0;
input = "FGS1=(B+A*10)+A*10+(C*10.5)";
Regex r = new Regex("([A-Z][A-Z\\d]*)");
bool f = false;
MatchEvaluator me = delegate(Match m)
{
  f = true;
  i++;
  return "i" + i.ToString();
};
do { f = false; input = r.Replace(input, me); } while (f);
//expected result: input == "i1=(i2+i3*10)+i4*10+(i5*10.5)"

But i have to do it in more complex way, for what i have to do something with found value. 但我必须以更复杂的方式做到这一点,因为我必须用已发现的价值做些什么。 For example: 例如:

MatchEvaluator me = delegate(Match m)
{
  foundValue = /*getting value*/;
  if (foundValue = "A") i--;
  f = true;
  i++;
  return "i" + i.ToString();
};

Expected result for this code: "i1=(i2+i2*10)+i2*10+(i3*10.5)" 此代码的预期结果: "i1=(i2+i2*10)+i2*10+(i3*10.5)"

You can use the Groups collection in the match object to get the matched groups. 您可以使用匹配对象中的Groups集合来获取匹配的组。 The first item is the entire match, so the value from the first group is at index 1: 第一项是整个匹配,因此第一组的值位于索引1处:

string foundValue = m.Groups[1].Value;
if (foundValue == "A") i--;

Guessing you need to implement a variable assignment in which you assign ix (where x is an incrementing number) to each variable and then reuse this value if it appears, we can write the following code to solve your problem: 猜测你需要实现一个变量赋值,在其中为每个变量分配ix(其中x是递增的数字),然后重用该值,如果它出现,我们可以编写以下代码来解决你的问题:

var identifiers = new Dictionary<string, string>();
int i = 0;
var input = "FGS1=(B+A*10)+A*10+(C*10.5)";
Regex r = new Regex("([A-Z][A-Z\\d]*)");
bool f = false;

MatchEvaluator me = delegate(Match m)
{
    var variableName = m.ToString();

    if(identifiers.ContainsKey(variableName)){
        return identifiers[variableName];
    }
    else {
        i++;
        var newVariableName = "i" + i.ToString();
        identifiers[variableName] = newVariableName;
        return newVariableName;
    }
};

input = r.Replace(input, me);
Console.WriteLine(input);

This code should print: i1=(i2+i3*10)+i3*10+(i4*10.5) 此代码应打印:i1 =(i2 + i3 * 10)+ i3 * 10 +(i4 * 10.5)

Your question should be answered by Guffa already, just want to share my alternative way to solve your problem, using more feature from .NET Regex (If I understand your problem correctly): 你的问题应该由Guffa回答,只想分享我的替代方法来解决你的问题,使用.NET Regex的更多功能(如果我正确理解你的问题):

int i = 1;
string input = "FGS1=(B+A*10)+A*10+(C*10.5)";   
var lookUp = new Dictionary<string, string>();
var output = Regex.Replace(input, 
             "([A-Z][A-Z\\d]*)", 
             m => { 
                if(!lookUp.ContainsKey(m.Value))
                {           
                    lookUp[m.Value] = "i" + i++;            
                }
                return lookUp[m.Value]; 
            });
Console.WriteLine(output);      //i1=(i2+i3*10)+i3*10+(i4*10.5)

I use a dictionary to keep track of which matched is repeated 我使用字典来跟踪重复的匹配

This should work even if your repeated matched is different from "A". 即使重复匹配与“A”不同,这也应该有效。 In your original solution, it checks specifically for "A", which is quite fragile 在您的原始解决方案中,它专门检查“A”,这是非常脆弱的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM