正则表达式匹配特定类型的字符串和替换

时间:2013-05-27 10:43:49

标签: c# regex string replace match

我的字符串包含 <link rid="bib*">222</link> <link rid="fig1">333</link> 现在我想用rid {“bib *”替换上面文本的所有内容,用<CITATION id="CD1">222</CITATION>替换所有出现的文本,用rid =“fig *”替换<FigRef id="fig*">222</FigRef>。< / p>

我试过这样的事情

var reg = new Regex(@"\<link rid=""bib(?<myText>.+?)""\>(?<myText2>.+?)\</link\>$");

但是我不知道如何继续这个并且卡住了。 请帮帮我。

2 个答案:

答案 0 :(得分:0)

这是一个基于非常相似的正则表达式的解决方案:

private const string REGEX_LINK = @"<link\s+rid=""([^""\d]+)(\d+)"">(.*?)</link>";
private const int REGEX_LINK_GRP_RID_NAME = 1;
private const int REGEX_LINK_GRP_RID_ID = 2;
private const int REGEX_LINK_GRP_VALUE = 3;

static void Main(string[] args)
{
    var testInputString = "I'm having a string with <link rid=\"bib123\">222</link> and <link rid=\"fig456\">333</link> now i want to replace all the occurances...";
    Regex linkFinder = new Regex(REGEX_LINK, RegexOptions.IgnoreCase);
    var result = linkFinder.Replace(testInputString, new MatchEvaluator(LinkMatchEvaluator));
    Console.WriteLine(result);
    Console.ReadKey();
}

private static string LinkMatchEvaluator(Match m)
{
    const string CITATION_RID_NAME = "bib";
    const string FIGREF_RID_NAME = "fig";

    var ridName = m.Groups[REGEX_LINK_GRP_RID_NAME].Value.ToLower();
    var ridID = m.Groups[REGEX_LINK_GRP_RID_ID].Value;
    var value = m.Groups[REGEX_LINK_GRP_VALUE].Value;
    if (ridName == CITATION_RID_NAME)
    {
        return String.Format("<CITATION id=\"CD{0}\">{1}</CITATION>", ridID, value);
    }
    else if (ridName == FIGREF_RID_NAME)
    {
        return String.Format("<FigRef id=\"fig{0}\">{1}</FigRef>", ridID, value);
    }
    return m.Value;
}

答案 1 :(得分:0)

感谢您的回复。最后,我找到了自己的问题的解决方案。现在我得到了解决问题的方法。我这样解决了。

   public enum intLinks
    {
        bib = 1,
        fig = 2,
        tab=3,
        tb=4
    }
  

这是用匹配模式替换内容的方法。

   public string NumberedReplaceTest(string input, intLinks refToFind)
    {
        //"<link rid=\"bib1\">1</link>"
        Regex regex = new Regex(@"<link rid=""" + refToFind.ToString() + @"(?<sequence>\d*)"">(\r?\n)*(?<number>[a-zA-Z0-9]*)(\r?\n)*</link>");
        if (!regex.IsMatch(input))
            return input;

        switch (refToFind)
        {
            case intLinks.bib: return regex.Replace(input, "<Citation CID=\"CR${sequence}\">${number}</Citation>");
            case intLinks.fig: return regex.Replace(input, "<InternalRef RefID=\"Fig${sequence}\">${number}</InternalRef>");
            case intLinks.tab: return regex.Replace(input, "<InternalRef RefID=\"Tab${sequence}\">${number}</InternalRef>");
            case intLinks.tb: return regex.Replace(input, "<InternalRef RefID=\"Tab${sequence}\">${number}</InternalRef>");
            default: return input;
        }            

     }
  

我正在调用这样的方法。

        bodyString = NumberedReplaceTest(bodyString,intLinks.bib);
        bodyString = NumberedReplaceTest(bodyString, intLinks.fig);
        bodyString = NumberedReplaceTest(bodyString, intLinks.tab);
        bodyString = NumberedReplaceTest(bodyString, intLinks.tb);`