C# 替换正则表达式匹配中出现的特定文本

C# 替换正则表达式匹配中出现的特定文本,c#,regex,C#,Regex,EDIT:此示例使用html,但我需要这种类型的场景来处理其他类型的字符串。请将此作为正则表达式问题阅读,而不是html问题 假设我有这样一个字符串: HelloWorld 我可能需要将文本替换为这些标题标记中的任何一个,但让我们使用这个示例,其中我只想修改,如下所示: HelloWorld 因为我可能需要替换任何标题,所以我只搜索对不起,不能用C#回答,但答案应该非常相似。对于您的特定情况,JavaScriptString.prototype.replace()的regexp属性是/()/,而

EDIT:此示例使用html,但我需要这种类型的场景来处理其他类型的字符串。请将此作为正则表达式问题阅读,而不是html问题

假设我有这样一个字符串:

HelloWorld

我可能需要将文本替换为这些标题标记中的任何一个,但让我们使用这个示例,其中我只想修改
,如下所示:

HelloWorld

因为我可能需要替换任何标题,所以我只搜索
对不起,不能用C#回答,但答案应该非常相似。对于您的特定情况,JavaScript
String.prototype.replace()
的regexp属性是
/()/
,而replacement属性是
“$1”
So

var str = "<h1>Hello</h1><h2>World</h2><h3>!</h3>",
 repStr = str.replace(/(<h1.+?\/h1>)/,'$1<div id="h2div"></div>');

console.log(repStr) // "<h1>Hello</h1><div id="h2div"></div><h2>World</h2><h3>!</h3>"
var str=“HelloWorld!”,
repStr=str.replace(/()/,“$1”);
console.log(repStr)/“HelloWorld!”
或者,如果您不想使用捕获组,您仍然可以这样做

var repStr = str.replace(/<h1.+?\/h1>/,'$&<div id="h2div"></div>');
var repStr=str.replace(//,'$&');

在这种特殊情况下,基本上会给出相同的结果。

使用MatchEvaluator

private static int count = 0;
    static string CapText(Match m)
    {
        count++;

        if (count == 2)
        {
            return "<div id=\"h2div\"></div>" + m.Value;
        }

        return m.Value;
    }

private void button1_Click()
{
    var htmlText = "<h1>Hello</h1><h2>World</h2><h3>!</h3>";
    Regex rx = new Regex(@"<h([1-6])");
    var result = rx.Replace(htmlText, new MatchEvaluator(ClassOfThis.CapText));
}
private static int count=0;
静态字符串CapText(匹配m)
{
计数++;
如果(计数=2)
{
返回“+m”值;
}
返回m.值;
}
专用无效按钮1\u单击()
{
var htmlText=“HelloWorld!”;

Regex rx=new Regex(@“我为此挣扎了一整天。自然地,问这个问题有时会激发创造力,所以这就是我提出的解决方案。它使用MatchCollection,然后使用字符串生成器插入字符串。字符串生成器在这方面可能有些过火,但它可以工作:-)

replacementIndex定义了要插入文本的匹配项。在我的例子中,正则表达式找到了三个实例并修改了找到的索引1。从那里,我得到了起始字符串索引,并使用子字符串插入文本。这只是一个按钮的测试代码,以验证其功能

    private void button1_Click(object sender, EventArgs e)
    {
        //sample text.
        var htmlText = "<h1>Hello</h1><h2>World</h2><h3>!</h3>";

        //the string builder will handle replacing the text.
        var stringBuilder = new StringBuilder(htmlText);

        //build the replacement text.
        var replacementString = "<div id=\"" + "h2div" + "\">" + "</div>";
        int replacementIndex = 1; //only replace the second occurence found by regex (zero-indexed).

        //find ALL occurrences of <h1 through <h6 in the file, but only replace <h2.
        var pattern = "<h([1-6])";
        MatchCollection matches = Regex.Matches(htmlText, pattern); //get all the matches.
        int startIndex = matches[replacementIndex].Index; //get the starting string index for the match.

        //insert the required text just before the found match.
        stringBuilder.Insert(startIndex, replacementString);

        //copy text to clipboard and display it on screen.
        htmlText = stringBuilder.ToString();
        System.Windows.Forms.Clipboard.SetText(htmlText);
        MessageBox.Show(htmlText);
    }
private void按钮1\u单击(对象发送者,事件参数e)
{
//示例文本。
var htmlText=“HelloWorld!”;
//字符串生成器将处理替换文本的操作。
var stringBuilder=新的stringBuilder(htmlText);
//构建替换文本。
var replacementString=“”+”;
int replacementIndex=1;//仅替换正则表达式找到的第二个匹配项(零索引)。

//查找所有出现的HTML。我建议使用HtmlAgilityPack而不是正则表达式来处理HTML。这只是一个示例。我有一些非HTML场景也需要这样做。感谢您的回复。查看您的代码,我认为您假设我只查找h2,但我正在查找正则表达式匹配中的第二个出现。我在其他情况下,我可能会寻找第三个匹配项。此外,字符串是一个示例,因此不能保证h2将紧跟在h1之后……我只想修改如下:
HelloWorld!
据我所知,您不想替换h2标记,但您正在插入一个id为
h2div
在h1和h2标记之间。这就是我所做的。如果我没有得到你想要的东西,很抱歉。您好。这样想吧。你可以找到任何类似于
的标记。我正在尝试测试它,但不清楚你所说的
//在这里做些什么
    private void button1_Click(object sender, EventArgs e)
    {
        //sample text.
        var htmlText = "<h1>Hello</h1><h2>World</h2><h3>!</h3>";

        //the string builder will handle replacing the text.
        var stringBuilder = new StringBuilder(htmlText);

        //build the replacement text.
        var replacementString = "<div id=\"" + "h2div" + "\">" + "</div>";
        int replacementIndex = 1; //only replace the second occurence found by regex (zero-indexed).

        //find ALL occurrences of <h1 through <h6 in the file, but only replace <h2.
        var pattern = "<h([1-6])";
        MatchCollection matches = Regex.Matches(htmlText, pattern); //get all the matches.
        int startIndex = matches[replacementIndex].Index; //get the starting string index for the match.

        //insert the required text just before the found match.
        stringBuilder.Insert(startIndex, replacementString);

        //copy text to clipboard and display it on screen.
        htmlText = stringBuilder.ToString();
        System.Windows.Forms.Clipboard.SetText(htmlText);
        MessageBox.Show(htmlText);
    }