有正则表达式高手在吗
<group id='1' name='gates'/>
如何使用指定的字串(如:bill)替换其中的 name 属性的值?

解决方案 »

  1.   

    ResultString = Regex.Replace(yourStr, "(<group\\s+id='[^']+'\\s+name=')[^']+('/>)", "$1bill$2");
      

  2.   

    using System;
    using System.Collections;using System.Text.RegularExpressions;public class MyClass
    {
    public static void Main()
    {
    String s = "<group id='1' name='gates'/>";

    String pattern = "(?<1>(<group\\s+id='(.)*'\\s+))name(?<2>(\\s*=\\s*'(.)*'\\/>))";
    Regex r = new Regex(pattern, RegexOptions.IgnoreCase);

    String result = r.Replace(s, "${1}bill${2}");

    Console.WriteLine(result);

    Console.ReadLine();
    }

    }
      

  3.   

    string s = "name = 'Bill'";
    string ss = Regex.Replace(s,@"(?<=name \= ')\w+(?=')","gates");
      

  4.   

    感谢 mobydick 准确的答案
    感谢 xrascal 的代码,可能代码有些误差,我没有试出来结果(对表达式我不太懂,说错了别拍我)
    感谢 JasonHeung 的答案
    谢谢三位的帮助