日期:2014-05-20  浏览次数:20682 次

急急急!正则帝进!
求一正则表达式 替换 "AA" "BB" 之间的 任何 "CC" 为 "DD"

例如 "AACCBBAAEFCCDBB" 变为 "AADDBBAAEFDDDBB"


多谢!


------解决方案--------------------
static void Main(string[] args)
{
Regex regex = new Regex(@"AA[a-zA-Z0-9_\u4e00-\u9fa5]+BB");
MatchEvaluator me = new MatchEvaluator(replace);
string str = "AACCBBCCAAEFCCDBB";
str = regex.Replace(str, me);
Console.WriteLine(str);
}
static string replace(Match m)
{
return m.Value.Replace("CC", "DD");
}
------解决方案--------------------
找到更好的方法了:
C# code

            Regex reg=new Regex( @"AA.*CC.*BB" );
            MatchCollection mc = reg.Matches( "AACCBBAAEFCCDBB" );
            MessageBox.Show( reg.Replace( "AACCBBAAEFCCDBB", new MatchEvaluator( ( m ) => {
                return m.Value.Replace( "CC", "DD" );
            } ) ) );

------解决方案--------------------
探讨

static void Main(string[] args)
{
Regex regex = new Regex(@"AA[a-zA-Z0-9_\u4e00-\u9fa5]+BB");
MatchEvaluator me = new MatchEvaluator(replace);
string str = "AACCBBCCAAEFCCDBB";
s……

------解决方案--------------------
C# code
string str = "AACCBBAAEFCCDBB";
            if (new Regex("AA.*BB").IsMatch(str)) 
            {
                str=str.Replace("CC", "DD");
                Console.WriteLine(str);
            }

------解决方案--------------------
C# code

void Main()
{
    string str = "AACCBBAAEFCCDBBCC";
 str=Regex.Replace(str,"(?<=AA).*?(?=BB)",m=>m.Value.Replace("CC","DD"));
 Console.WriteLine(str);
 //AADDBBAAEFDDDBBCC
}