0

我有一个字符串,<link rid="bib*">222</link>现在<link rid="fig1">333</link> 我想用 rid="bib*" 替换上述文本的所有出现,<CITATION id="CD1">222</CITATION>并用 rid="fig*" 替换所有出现的文本,用<FigRef id="fig*">222</FigRef>.

我试过这样的东西

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

但我不知道如何进行此操作并卡住了。请帮我解决一下这个。

4

2 回答 2

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);` 
于 2013-05-29T14:31:02.477 回答
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;
}
于 2013-05-27T14:08:00.880 回答