正则表达式无法匹配字符串
我在下面有一段代码,我试图用它来匹配字符串的开头和结尾,其中中间可以改变。我首先尝试让这个例子工作,有人可以告诉我这个代码的错误以及为什么它根本不匹配。
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要分隔符,在 c# 中您只需指定正则表达式:
You don't need the delimiters, in c# you just specify the Regex:
如果只有相关字符串的中间部分会发生变化,那么为什么不使用 String.StartsWith 和 String.EndsWith 呢?例如:
If only the middle portion of the string in question is subject to change, then why not use
String.StartsWith
andString.EndsWith
? For example: