如题,本人新学习C#有很多方法不太明白,有一个文本文件,里面有很多“()”括号,想提取出括号里面的内容,求几个解决方法!
谢谢各位了!
------解决方案--------------------
- C# code
string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取tx string pattern = @"(?<=\()[^)]+(?=\))"; string[] result = Regex.Matches(tempStr, pattern).Cast<Match>().Select(a => a.Value).ToArray(); /* * [0] "../images/changpinlist_01.jpg" string [1] "../images/changpinlist_02.jpg" string [2] "../images/changpinlist_03.jpg" string */
------解决方案--------------------
C:\Css.txt是你的文本内容
- C# code
string CssStr = File.ReadAllText(@"C:\Css.txt", System.Text.Encoding.GetEncoding("GB2312")); List<string> list=new List<string>(); string pattern = @"(?is)(?<=\()[^()]*?(?=\))"; foreach(Match m in Regex.Matches(CssStr , pattern)) { list.Add(m.Value);//结果接在list中 }