C#正则表达式难题 [英] C# Regex Pattern Conundrum

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

问题描述

我有我的3个独立的来源已经证实为顺利达到要求的文本的正则表达式。

I have a regex that I've verified in 3 separate sources as successfully matching the desired text.


  1. http://regexlib.com/RETester.aspx

  2. http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression -tester.ashx

  3. http://sourceforge.net/projects/regextester /

  1. http://regexlib.com/RETester.aspx
  2. http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx,
  3. http://sourceforge.net/projects/regextester/

但是,当我用我的代码的正则表达式。它不产生一个匹配。我用正则表达式的其他与此代码,他们已经导致了所需的匹配。我不知所措......

But, when I use the regex in my code. It does not produce a match. I have used other regex with this code and they have resulted in the desired matches. I'm at a loss...

string SampleText = "starttexthere\r\nothertexthereendtexthere";
string RegexPattern = "(?<=starttexthere)(.*?)(?=endtexthere)";
Regex FindRegex = new Regex(@RegexPattern);
Match m = FindRegex.Match(SampleText);



我不知道这个问题是我的正则表达式,或者我的代码。

I don't know if the problem is my regex, or my code.

推荐答案

的问题是,您的文本包含 \r\\\
这意味着它被分为两行。如果你想整个字符串匹配,你必须设置选项以多行匹配,并修改包含 \\\
(换行字符)的匹配

The problem is that your text contains a \r\n which means it is split across two lines. If you want to match the whole string you have to set the option to match across multiple lines, and to change the behavior of the . to include the \n (new-line character) in matched

 Regex FindRegex = new Regex(@RegexPattern, RegexOptions.Multiline | RegexOptions.Singleline);

这篇关于C#正则表达式难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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