日期:2014-05-17  浏览次数:20494 次

求正则表达式分割字符串的思路
一个字符串:str = "(1) P ((2)@!)";
期望分割成一个数组array
arr[0] = (1)
arr[1] = P
arr[2] = (2)
arr[3] = @!

请问应该如何处理?
补充 (1)是一个整体,里面可以是(3)甚至(50)等等

在线等 谢谢
 
正则表达式

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "(1) P ((2)@!)";
            Match m = Regex.Match(str, @"(\(\d+\))\s([A-Z]+)\s\((\(\d+\))(.+)\)");
            string[] result = new string[] { m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value, m.Groups[4].Value };
            foreach (var item in result) Console.WriteLine(item);
        }
    }
}