1

In following code, why is the pattern not matched by the regular expression, but it is properly working if I am reading the 12sep.txt file line by line using regular expression?

string file = @"C:\Documents and Settings\Sandeep.kumar\Desktop\12sep.txt";
string filedta = File.ReadAllText(file);
string pattern = @"^[^\s]+.[^\s]txt$";
Regex rx = new Regex(pattern, RegexOptions.None);
MatchCollection mc = rx.Matches(filedta);
4

1 に答える 1

11

正規表現の特殊文字「^」と「$」は、入力文字列が単一行または複数行の場合、同じ意味を持ちません。1 行では、「文字列の開始」と「文字列の終了」を意味します。複数行では、「行の開始」と「行の終了」を意味します。

これを制御するオプションが RegexOptions にあります。RegexOptions.Multiline

ドキュメントには、それが何をするのかが明確に記載されています。

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspxから

于 2012-09-13T10:08:06.677 に答える