在C#中如何用正则提取"@RA@YA@L2@Q2@EA"中的@前面第一个值和第二个值,如
提取后变成RYLQE和AA22A

解决方案 »

  1.   

    有个简单的办法,
    首先置换掉所有@符号
    然后根据string的index来取需要的字符,基数和偶数的组合
      

  2.   

     static void Main(string[] args)
            {
                string s="@RA@YA@L2@Q2@EA".Replace("@","");
                string s1 = "",s2="";
                for (int i = 0; i < s.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        s1 += s[i];
                    }
                    else
                    {
                        s2 += s[i];
                    }
                }
                Console.WriteLine(s1);
                Console.WriteLine(s2);
                Console.Read();
            }
      

  3.   

                string html = @"@RA@YA@L2@Q2@EA";
                Regex reg = new Regex(@"(?<=\@(?<s1>\w{1}))\w{1}");
                MatchCollection mc = reg.Matches(html);
                Console.WriteLine("/*\n------输出结果------------");
                string s1 = "";
                string s2 = "";
                foreach (Match m in mc)
                {
                    s1 += m.Groups["s1"].ToString();
                    s2 += m.Groups[0].ToString();
                }            Console.WriteLine(s1+"\n"+s2);
                
                
                Console.WriteLine("*/");            /*
                ------输出结果------------
                RYLQE
                AA22A
                */
      

  4.   

    using System;
    using System.Text.RegularExpressions;class Program
    {
      static void Main()
      {
        string s = "@RA@YA@L2@Q2@EA";
        string s1 = Regex.Replace(s, @"@(.).", "$1");
        string s2 = Regex.Replace(s, @"@.(.)", "$1");
        Console.WriteLine(s1);  // 输出:RYLQE
        Console.WriteLine(s2);  // 输出:AA22A
      }
    }
      

  5.   

    Dim html As String = "@RA@YA@L2@Q2@EA"Dim reg As Regex = New Regex("@(?<first>.{1})(?<second>.{1})")
    Dim s1 As String = reg.Replace(html,"${first}")
    Dim s2 As String = reg.Replace(html,"${second}")<first>.{1}:红色表示匹配字符的个数,可以根据需要修改。