發問發問
 

已答覆請教正規表示式寫法

  • 2008年5月13日 下午 07:14jeryeu 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    各位前輩們好
                                                                                   
    小弟想利用正規表示式來抓取想要的字串                                                                               
    但是怎麼TRY都不成功                                                                             
    可以麻煩大大們幫我看看哪裡錯誤嗎?
                                                                                   
    問題如下:
     position = 0 type = 1 token = c
     position = 1 type = 2 token = +
     position = 2 type = 2 token = +
     position = 3 type = 4 token = 程式
     position = 7 type = 4 token = 語言
    ^           ^  ^  ^     ^  ^  ^        ^ ^     ^
    |            |  |   |     |   |  |         | |      |
    tab        |  |  tab  |   | tab       | |      ewline
                |  |         |   |            | |
             space    space        space
                                                                                   
    小弟想抓Coffee [+] [+] [程式] [語言] 這六個字串                                                                               
    自己霧裡看花  亂寫了一通  

     

    \tposition\s=\s[0-9]+\ttype\s=\s[0-9]+\ttoken\s=\s

     

    拜託各位了 感激不盡!!!


     

解答

  • 2008年5月13日 下午 11:44chhuang 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    string [] s = {"\tposition = 7\ttype = 4\ttoken = 語言\r\n",
                   "\tposition = 1\ttype = 2\ttoken = +\r\n",
                   "\tposition = 2\ttype = 2\ttoken = +\r\n",
                   "\tposition = 3\ttype = 4\ttoken = 程式\r\n",
                   "\tposition = 7\ttype = 4\ttoken = 語言\r\n"
                  };
     
    foreach (string ss in s)
    {
        foreach( Match m in Regex.Matches(ss, @"(\S+) = (\S+)"))
        {
            Console.Write("{0} = {1},", m.Groups[1], m.Groups[2]);
        }
        Console.WriteLine();
    }
  • 2008年5月14日 上午 03:36Johnny.NetMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    若如你描述的情況, 沒有比使用 Regex 搭配字串陣列更適合的了。

     

    Code Snippet

     

    string input = @" position = 0 type = 1 token = c
     position = 1 type = 2 token = +
     position = 2 type = 2 token = +
     position = 3 type = 4 token = 程式
     position = 7 type = 4 token = 語言";
    string regFormat = @"^(\sposition\s=\s(\d)+\stype\s=\s(\d)+\stoken\s=)";
    string[] seperated = Regex.Replace(input, regFormat, ",", RegexOptions.Multiline).Split(',');
    foreach (string retrieved in seperated)

    {

      if (!String.IsNullOrEmpty(retrieved))

        Response.Write(retrieved + ", ");

    }

     

     

    我把你的 pattern 稍為改了一下; 如果你對 Regex 不是很熟的話, 可以看看「[Regex] Regular Expression 初論」。

所有回覆

  • 2008年5月13日 下午 11:44chhuang 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    string [] s = {"\tposition = 7\ttype = 4\ttoken = 語言\r\n",
                   "\tposition = 1\ttype = 2\ttoken = +\r\n",
                   "\tposition = 2\ttype = 2\ttoken = +\r\n",
                   "\tposition = 3\ttype = 4\ttoken = 程式\r\n",
                   "\tposition = 7\ttype = 4\ttoken = 語言\r\n"
                  };
     
    foreach (string ss in s)
    {
        foreach( Match m in Regex.Matches(ss, @"(\S+) = (\S+)"))
        {
            Console.Write("{0} = {1},", m.Groups[1], m.Groups[2]);
        }
        Console.WriteLine();
    }
  • 2008年5月14日 上午 03:36Johnny.NetMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    若如你描述的情況, 沒有比使用 Regex 搭配字串陣列更適合的了。

     

    Code Snippet

     

    string input = @" position = 0 type = 1 token = c
     position = 1 type = 2 token = +
     position = 2 type = 2 token = +
     position = 3 type = 4 token = 程式
     position = 7 type = 4 token = 語言";
    string regFormat = @"^(\sposition\s=\s(\d)+\stype\s=\s(\d)+\stoken\s=)";
    string[] seperated = Regex.Replace(input, regFormat, ",", RegexOptions.Multiline).Split(',');
    foreach (string retrieved in seperated)

    {

      if (!String.IsNullOrEmpty(retrieved))

        Response.Write(retrieved + ", ");

    }

     

     

    我把你的 pattern 稍為改了一下; 如果你對 Regex 不是很熟的話, 可以看看「[Regex] Regular Expression 初論」。

  • 2008年5月14日 上午 04:03jeryeu 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    非常感謝兩位前輩的指導

    正規表示式果然是非常實用的東西

    等下來試試看Smile