求一正则表达式,匹配一个字符串中被空格分隔开的所有单词,如"bike jeep sheep jeep",匹配"bike","jeep","sheep","jeep"但不匹配空格。

解决方案 »

  1.   

    用这个:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;namespace ConsoleApplication6
    {
        class Program
        {
            //要注意的是,只有圆括号“()”才能用于形成组。“[]”用于定义字符集。“{}”用于定义重复操作。
            //正则导向的引擎总是返回最左边的匹配,文本导向匹配最右边            private static string rex = @"^[a-zA-Z]+|\s[a-zA-Z]+";
            static void Main(string[] args)
            {
                //你可以对相同的后向引用组进行多次引用,<<([a-c])x\1x\1>>将匹配“axaxa”、“bxbxb”以及“cxcxc”。如果用数字形式引用的组没有有效的匹配,则引用到的内容简单的为空。
                //fo+是替换foo fooo 等
                //fo{1,2}是替换f后面出现的1-2个o
                //\s是匹配空格,\S是匹配除单个空格符之外的所有字符,\d用于匹配从0到9的数字
                //.用于匹配除换行符之外的所有字符
                //^定位符规定匹配模式必须出现在目标字符串的开头
                //$定位符规定匹配模式必须出现在目标对象的结尾?
                //否定符 [^]
                //字符集是由一对方括号“[]”括起来的字符集合"a[34]b"匹配a3b或a4b
                //<<[\\x]>>将会匹配一个反斜杠和一个X
                Vis_Reg();
            }        //如果在正则后加个+        private static void Vis_Reg() 
            {
                string str = Console.ReadLine();
                string reg = Console.ReadLine();
                string[] Split_Str = new string[]{ };
                if (Regex.IsMatch(str, rex))
                {
                    Regex regex = new Regex(rex);
                    //Split_Str = regex.Split(str);
                    //foreach(string str_s in Split_Str)
                    //{
                    //    Console.Write(str_s + ",");
                    //}
                    //Console.Write("\n");                Console.Write(regex.Replace(str, reg) + "\n");   
                    Vis_Reg();
                }            
            }
        }
    }
      

  2.   

    http://regexlib.com/Search.aspx 自己有空可以看看这个网站.里面有很多的介绍和示例的.