有如下文本
iaaaaaaaaaax
cdscccccccc
ssssssss
ibbbbbbbbbbx
isdfsfasdfx我想得到以i开头,x结尾的行,请问如何书写我写勒一个正则表达式并启动的多行选项
string pattern="^i\w+x$";
但只能得到一个值,尝试了string pattern="^i\w+?x$"也是不行谢谢帮忙

解决方案 »

  1.   

    string pattern=@"^i[\w]*?x$";
    用非贪婪模式匹配
      

  2.   

    用Regex.Matchs
    返回类型为
    System.Text.regularExpressions.MatchCollenction遍历它取出每一个匹配
      

  3.   

    jingtao_zhou(小熊)你的写法也不行啊,
    我写的第2个模式也是的也是非贪婪匹配啊jingtao_zhou(小熊)string path=@"C:\Documents and Settings\j\桌面\1.txt";
    StreamReader sr=new StreamReader(path); string str=sr.ReadToEnd();


                    string pattern="^i\w+x$";//
    // string  pattern=@"^i\w+?x$";

    MatchCollection     mc=Regex.Matches(str,pattern,RegexOptions.Multiline);
    Console.Write(mc.Count);
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    namespace ReadFile
    {
        class Reader
        {
            StreamReader read; 
            static void Main(string[] args)
            {
                StreamReader read = new StreamReader("d:\\source.txt");
                String line;
                try
                {
                    while ((line = read.ReadLine()) != null)
                    {
                        if (line.Substring(0, 1) == "i" || line.Substring(line.Length - 1, 1) == "x")
                            Console.WriteLine(line);
                    }
                    read.Close();
                }
                catch (Exception ex)
               {
                   Console.WriteLine(ex.Message);
                }
            }
        }
    }你试一下,你是要这个结果吗!
      

  5.   

    string str=sr.ReadToEnd();
    是把整个文本内容读出来string  pattern=@"^i\w+?x$";
    你用这个模式取匹配是去匹配第一个字符是i,最后一个字符是x
    因为是整个内容去匹配,要么存在要么没有
    如果你想按行去匹配
    改成string  pattern=@"^i.+?x$";
      

  6.   

    写错了string  pattern=@"i[\w]+?$";
      

  7.   

    回:jingtao_zhou(小熊) 

    写错了string pattern=@"i[\w]+?$";这个也错了吧...
      

  8.   

    其实用正则表达式跟stswordman的做法,效率都差不多的
      

  9.   

    to:stswordman(糕) 
    string pattern=@"i[\w]+?$";
    呵呵,应该是
    string pattern=@"i[\w]*?x";改成*这样ix也能被匹配进去
      

  10.   

    回:jingtao_zhou(小熊) 
    嗯,行了
    请问这个个语句有啥区别啊???我觉得一样啊
    string pattern=@"i[\w]*?x"
    string pattern="^i\w+?x$"
      

  11.   

    还有string pattern=@"i[\w]+?x"
    都是非贪婪的啊?
      

  12.   

    在我这个程序里>=1和>=0没有区别啊
      

  13.   

    string path=@"C:\1.txt";
    StreamReader sr=new StreamReader(path);

    string str=sr.ReadToEnd();

    string pattern= @"i\w*?x";

    MatchCollection m = Regex.Matches(str, pattern);
    Console.Write(m.Count);改了两个地方,貌似可以了
      

  14.   

    还没解决啊MatchCollection mc = Regex.Matches(str, pattern);
    for(int i = 0; i < mc.Count; i++)
    {
    Console.WriteLine(mc[i].Groups[1].Value);
    }
    差不多这样,你参考参考
      

  15.   

    string pattern=@"^i[\w]*?x$";
    用非贪婪模式匹配
      

  16.   

    string path=@"C:\Documents and Settings\j\桌面\1.txt";
    StreamReader sr=new StreamReader(path); string str=sr.ReadToEnd();


                    string pattern="^i\w+x$";//
    // string  pattern=@"^i\w+?x$";

    MatchCollection     mc=Regex.Matches(str,pattern,RegexOptions.Multiline);
    Console.Write(mc.Count);
      

  17.   

    各位真不好意思,刚才晕了,连我自己都忘了需求了。请看一下我的需求:
    要求:到以i开头,x结尾的行而不是包含i和x的行,谢谢
      

  18.   

    是我就会自己写个SUB......
    .......
      

  19.   

    文本里面单词  \bi\S*\bx
    文本 ^i\S*x$
      

  20.   

    呵呵,问题解决了,因为文本输入的换行是\r\n,而启动正则表达式的多行选项只能比配单个的结束符,所以造成了无法正确匹配的现象。解决的方法是在查找前将文本中的\r\n替换成其中的一个
    \r或者\n什么的