現在C#で正規表現を使い、2つの特定の文字列の間にある文字列を取得するプログラムを組んでいます。
using System.Text.RegularExpressions;
/*--省略--*/
string str = "aaabccccc"; // 元の文字列
Regex reg = new Regex("aaa(?<text>.*?)cc", // ここでは「b」がtextに入る
RegexOptions.IgnoreCase | RegexOptions.Singleline);
string value = "";
for (Match m = reg.Match(str); m.Success; m = m.NextMatch())
{
value = m.Groups["text"].Value;
MessageBox.Show(value); // 出力:b
}
/*--省略--*/
using System.Text.RegularExpressions;
/*--省略--*/
string str = "aaa[b]"; // 元の文字列
Regex reg = new Regex("aaa[(?<text>.*?)]", // ここでは「b」がtextに入るはず
RegexOptions.IgnoreCase | RegexOptions.Singleline);
string value = "";
for (Match m = reg.Match(str); m.Success; m = m.NextMatch())
{
value = m.Groups["text"].Value;
MessageBox.Show(value);
}
/*--省略--*/
これはどういう理由でこうなるのでしょう?
また、解決策はあるのでしょうか?
宜しくお願いします。