c#正則表達式截取關鍵字符
妳寫的正則表達式沒有錯,是不是妳的漢字編碼有問題.我給妳寫了壹個C sharp的完整程序,中文顯示沒有問題,妳看看吧.
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Text.RegularExpressions;
namespace?ConsoleApplication3
{
class?Program
{
static?void?Main(string[]?args)
{
string?s?=?"[1]張小青.風電機組防雷與接地.[M].北京中國電力出版社,2009.\n[2]王麗廣.風電機組的防雷保護[D].湖南中國南車集團株洲電力機車研究所,2008.?\n[3]康春華,張小青,王芳.風電機組的防雷問題[D].北京北京交通大學新能源研究所,2006.?\n[4]林誌遠,黃聰.風力發電機組的防雷問題[D].廣東電力,2001,14(5)15-18.?\n[5]李景祿.關於降阻劑在接地工程應用方面的探討[D].湖南長沙電力學院,2002.?\n[6]李亦倫.荷蘭VESTAS風機的防雷保護[D].內蒙古內蒙古風力發電總公司,1999.?\n";
Regex?r?=?new?Regex("](?<name>.*?)\\.(?<title>.*)\\.(?<where>.*?)[\\,|\\,](?<date>.*?)\\.");
MatchCollection?matches?=?r.Matches(s);
for?(int?i?=?0;?i?<?matches.Count;i++)
{
if?(matches[i].Success)
{
Console.WriteLine(matches[i].Groups["name"].Value);
Console.WriteLine(matches[i].Groups["title"].Value);
Console.WriteLine(matches[i].Groups["where"].Value);
Console.WriteLine(matches[i].Groups["date"].Value);
}
}
Console.ReadKey();
}
}
}