现在有一字符串比如"IBM发布基于人脑特性设计的全新计算架构和编程语言";怎么通过正则表达式得到一组为5个字符长的字符串数组,实例结果为:
"IBM发布",
"基于人脑特",
"性设计的全",
"新计算架构",
"和编程语言"。
望解析...谢谢正则表达式

解决方案 »

  1.   


    var value = Regex.Matches("IBM发布基于人脑特性设计的全新计算架构和编程语言", @"\w{5}");
      

  2.   

    话说你这五个一截取也用不到正则呀,Substring足以了using System;
    public class SubStringTest
    {
        public static void Main()
        {
            string[] info = { "Name: Felica Walker", "Title: Mz.", "Age: 47", "Location: Paris", "Gender: F" };
            int found = 0;
            Console.WriteLine("The initial values in the array are:");
            foreach (string s in info)
                Console.WriteLine(s);
            Console.WriteLine("{0}We want to retrieve only the key information. That is:", Environment.NewLine);
            foreach (string s in info)
            {
                found = s.IndexOf(":");
                Console.WriteLine(s.Substring(found + 1).Trim());
            }
        }
    }
    另外我把正则的例子也给你个using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text.RegularExpressions;namespace cop
    {
        class HelloWorld 
        {
            public static void Main(String[] args)
            {            string test = "123abc";            Regex reg = new Regex(@"[a-z]", RegexOptions.IgnoreCase);            string str = "";            MatchCollection mc = reg.Matches(test);            foreach (Match m in mc)
                {                str += m.Value + "\n";            }            Console.WriteLine(str);            /*
                 *输出结果 a b c
                 */            Console.ReadKey();        }
        }    
    }
      

  3.   

    一句话搞定:
    string[] result=Regex.Split(yourstr,"(?<=\\G.{5})");具体可参考我的blog:  http://blog.csdn.net/q107770540/article/details/6436973
      

  4.   

    言简意赅,好帅气,喜欢!
    所谓条条大路通罗马,二楼的也是可以的,只是需要处理一下:
    string[] = Regex.Matches("IBM发布基于人脑特性设计的全新计算架构和编程语言", @"\w{5}").Cast<Match>().Select(m=>m.Value).ToArray();有时间可以研究一下正则,才会发现正则的真正魅力.
      

  5.   

    纯LINQ的来了:
    string str="IBM发布基于人脑特性设计的全新计算架构和编程语言";
    var result=str.ToCharArray().GroupBy(x=>str.IndexOf(x)/5).Select(x=>new string(x.ToArray()));
      

  6.   

    update:9L的在找 INDEX时会有BUG,这样才更准确:
    string str="IBM发布基于人脑特性设计的全新计算架构和编程语言";
        var result=str.ToCharArray().Select((x,i)=>new {x,i}).GroupBy(x=>x.i/5).Select(g=>new string(g.Select(s=>s.x).ToArray()));