您的位置:首页 > 其它

用正则表达式 替换 分割字符串

2008-11-11 23:42 330 查看
普通方之一法提取 123{abc}465{xyz}789 中的{}中数据

 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "123{abc}465{xyz}789";
            string str2 = "";
            foreach (char ch in str1)
            {
                if (ch >= 97 && ch <= 123)
                {
                    str2 = str2 + ch;
                }
            }
            string[] s = str2.Split('{');
            str2 = string.Empty;
            for (int i = 1; i < s.Length; i++)//s[0]=null
            {
                str2 += s[i] + "/r/n";
            }
            Console.WriteLine(str2);
        }
    }
}

 

 

正则提取

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Regex reg = new Regex("(?<={)[^}]*(?=})");//或者用(?<={).*?(?=})
            string str = "123{abc}465{xyz}789";
            string result = string.Empty;
            foreach (Match m in reg.Matches(str))
            {
                result += m.Value + "/r/n";
            }
            Console.Write(result);
           
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息