正则表达式无法匹配字符串 [英] Regex fails to match string

查看:87
本文介绍了正则表达式无法匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一段代码,我试图用它来匹配中间可以改变的字符串的开头和结尾.我首先试图让这个例子工作,有人可以告诉我这段代码的错误以及为什么它根本不匹配.

I have a piece of code below which I am trying to use to match the start and the end of a string where the middle can change. I am first trying to get this example working could someone please tell me the error with this code and why it is not matching at all.

      string pattern = @"/\/>[^<]*abc/";
      string text = @"<foo/> hello first abc hello second abc <bar/> hello third abc";
      Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
      Match m = r.Match(text);

推荐答案

你不需要分隔符,在 c# 中你只需要指定正则表达式:

You don't need the delimiters, in c# you just specify the Regex:

  string pattern = @"\/>[^<]*abc";


  string text = @"<foo/> hello first abc hello second abc <bar/> hello third abc";


  Regex r = new Regex(pattern, RegexOptions.IgnoreCase);


  Match m = r.Match(text);

这篇关于正则表达式无法匹配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆