日期:2014-05-18  浏览次数:20810 次

Regex.Match方法的问题
Match titleMatch = Regex.Match(textBox1.Text, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
  string filetitle;
  string filetitle2;
  filetitle = titleMatch.Groups[1].Value;
  filetitle2 = titleMatch.Groups[2].Value;

我想将textBox1.text中内容的<title></title>内的内容提取出来,分别放到filetitle,和filetit2这两个变量中,可是用上述方法无法实现,只能取出一个来放到filetitle变量中,filetitle2变量是空的,请问我错在哪了,希望指点一下,还有一个问题就是RegexOptions.Multiline这个属性有什么用?先谢谢了!

------解决方案--------------------

Multiline 多行模式。更改 ^ 和 $ 的含义,使它们分别在任意一行的行首和行尾匹配,而不仅仅在整个字符串的开头和结尾匹配。


给你一个例子
#region methods

private string getMatchstring(string reg,string src)
{
string strResult="";
System.Text.RegularExpressions.Regex matchesRegex = new System.Text.RegularExpressions.Regex(reg,RegexOptions.IgnoreCase);

System.Text.RegularExpressions.MatchCollection matchesFound= matchesRegex.Matches(src);
String nextMatch="111-------------------------\r\n";
System.Text.StringBuilder resultString=new System.Text.StringBuilder(64);
ArrayList myArray= new ArrayList();
myArray.Clear();

for( int i=0;i<matchesFound.Count;i++)
{
myArray.Add(matchesFound[i]);
}
for(int k=0;k<myArray.Count;k++)
{
strResult+=myArray[k]+k.ToString()+"___"+nextMatch;
}
return strResult;
}
#endregion