0

複数行の文字列を返すメソッドがあります..パラメータを使用してテストしました.パラメータなしで(定数を使用して)動作します..ここに関数があります

public string AppComments()
        {
           string teststring = @"Nelly Thomas (Approve) 12/27/2012 8:50 PM - 12/27/2012 8:52 PM
                               (Nelly Thomas) LazyApproval by nelly.thomas@joshworld.local Approved

                                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
                                when an unknown printer took a galley of type and scrambled it to make a type specimen book";

              Regex reg = new Regex(@"(.+)\(.+\)\s((\d\d\/){2}\d{4}\s\d{1,2}:\d\d\s\w\w)\s-\s.+[\n|\r].+[\n|\r]{2}((?:.|\n)+)", RegexOptions.IgnoreCase | RegexOptions.Multiline);

              string returnstring = reg.Match(teststring).Groups[1].Value + reg.Match(teststring).Groups[2].Value + reg.Match(teststring).Groups[4].Value.ToString();

            return returnstring;
        }

しかし、次のテキストを渡すと、値が返されません..空白に見えます..メソッド内で渡している値には複数行のリテラルがないと思いますか?

Nelly Thomas (Approve) 12/27/2012 8:50 PM - 12/27/2012 8:52 PM
(Nelly Thomas) LazyApproval by nelly.thomas@joshworld.local Approved

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book

ここにパラメータを持つ関数があります

public string AppComments(string mystring)
        {

              Regex reg = new Regex(@"(.+)\(.+\)\s((\d\d\/){2}\d{4}\s\d{1,2}:\d\d\s\w\w)\s-\s.+[\n|\r].+[\n|\r]{2}((?:.|\n)+)", RegexOptions.IgnoreCase | RegexOptions.Multiline);

              string returnstring = reg.Match(mystring).Groups[1].Value + reg.Match(mystring).Groups[2].Value + reg.Match(mystring).Groups[4].Value.ToString();

            return returnstring;
        }
4

1 に答える 1

0

$を正規表現の行末の述語として使用してみてください。^ と $ を使用しない場合、RegexOptions.Multiline は意味がありません

別の方法: 行末をオプションとしてマークしてみてください:

Regex reg = new Regex(@"(.+)\(.+\)\s((\d\d\/){2}\d{4}\s\d{1,2}:\d\d\s\w\w)\s-\s.+[\n|\r]??.+([\n|\r]{2})??((?:.|\n)+)", RegexOptions.IgnoreCase);

于 2013-01-10T11:40:52.643 に答える