正则表达式示例

来源:互联网 发布:java 拦截器列子 编辑:程序博客网 时间:2024/05/16 11:40

用正则表达式取出包含在 <TABLE> </TABLE>中的 <book> </book>和 <teach> </teach>

static void Main(string[] args)
        {
            string str = "<table> <book> <row xh=/"74/"> <count>1 </count> <price>43.00 </price> </row> <row xh=/"80/"> <count>1 </count> <price>43.00 </price> </row> </book> <teach> <row xh=/"80/"> <count>1 </count> <price>43.00 </price> </row> </teach> </table>";
            Regex reg = new Regex(@"<book>(.*?)</book>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            Regex reg1 = new Regex(@"<teach>(.*?)</teach>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            MatchCollection matches = reg.Matches(str);
            foreach (Match match in matches)
            {
                Console.WriteLine(match.Groups[1].Value);
            }
            MatchCollection matches1 = reg1.Matches(str);
            foreach (Match match in matches1)
            {
                Console.WriteLine(match.Groups[1].Value);
            }

原创粉丝点击