.NET正则表达式点阵字符匹配回车?点阵、字符、正则表达式、NET

2023-09-04 00:38:30 作者:用心聆听难受

正则表达式我曾经使用过的每个单味一直有。字符匹配的一切,但新行(\ r或\ N)...当然,除非你启用单行标志。

Every single flavor of regex I have ever used has always had the "." character match everything but a new line (\r or \n)... unless, of course, you enable the single-line flag.

所以,当我尝试下面的C#code当时我震惊了:

So when I tried the following C# code I was shocked:

Regex rgx = new Regex(".");
if (rgx.Match("\r\n").Success)
  MessageBox.Show("There is something rotten in the state of Redmond!");

有显示该消息。只是,以确保我不会疯了,我想下面的JavaScript code:

It showed the message. Just to make sure I wasn't going insane, I tried the following JavaScript code:

if (/./.test("\r\n"))
  alert("Something's wrong with JavaScript too.");

中的JavaScript并没有显示信息,这意味着它也正是因为它的工作应该。

The JavaScript didn't show the message, meaning it's working exactly as it should.

显然,。字符.NET是匹配的\ R字符。我查了文档,看看它提什么:

Apparently, the "." character in .NET is matching the "\r" character. I checked the documentation to see if the mention anything about it:

通配符:匹配任何单个字符   除了的\ n。

Wildcard: Matches any single character except \n.

哇......从什么时候一个正则表达式的味道的永远的有斑点的匹配回车?你可能会认为.NET会表现得像的正则表达式的口味所有的休息...特别是因为它是在Windows环境中,它使用\ r \ N作为行分隔符。

Wow... since when does a Regex flavor ever have the dot match a carriage return? You would think .NET would behave like all the rest of the Regex flavors... especially because it's in a Windows environment which uses "\r\n" as line delimiters.

有没有什么标志/设置我可以启用,使其工作,因为它在其他的正则表达式的口味?有没有其他解决办法不涉及与替换所有字符[^ \ r \ n]的

Is there any flag/setting I can enable to make it work as it does in other Regex flavors? Are there any alternative solutions which don't involve replacing all . characters with [^\r\n]?

推荐答案

我写的正则表达式的英雄时,碰到了同样的问题。这是一个有点怪异。我在博客有关问题的这里。这导致了我加入了功能测试仪开启/关闭CRLFs。总之,由于某种原因,微软选择使用\ N(换行)来标记行尾。

I ran into this same issue when writing Regex Hero. It is a little bizarre. I blogged about the issue here. And that led to me adding a feature to the tester to enable/disable CRLFs. Anyway, for some reason Microsoft chose to use \n (line feeds) to mark line endings.

(更新)原因必须与这样的:

(UPDATE) The reason must be related to this:

微软.NET Framework正   EX pressions将最   其他常规受欢迎的功能   EX pression实现,比如   那些在Perl和awk。 设计为   与Perl 5正兼容   EX pressions ,.NET Framework正   EX pressions包括功能尚未   见于其他实现方式中,如   从右到左匹配和上即时   汇编。   http://msdn.microsoft.com/en-us/library/hs600312.aspx

Microsoft .NET Framework regular expressions incorporate the most popular features of other regular expression implementations such as those in Perl and awk. Designed to be compatible with Perl 5 regular expressions, .NET Framework regular expressions include features not yet seen in other implementations, such as right-to-left matching and on-the-fly compilation. http://msdn.microsoft.com/en-us/library/hs600312.aspx

和作为伊戈尔指出,Perl有相同的行为。

And as Igor noted, Perl has the same behavior.

现在,在单线和多行RegexOptions改变行为根据各地点和换行。您可以启用SINGLELINE RegexOption使点匹配换行符。并且,可以使多行RegexOption以便^和$标记每一行的开始和结束(通过线表示饲料)。但你不能改变的点(。)运算符的内在行为,以适应一切除了\ r \ñ。

Now, the Singleline and Multiline RegexOptions change behavior based around dots and line feeds. You can enable the Singleline RegexOption so that the dot matches line feeds. And you can enable the Multiline RegexOption so that ^ and $ mark the beginning and end of every line (denoted by line feeds). But you can't change the inherent behavior of the dot (.) operator to match everything except for \r\n.

 
精彩推荐
图片推荐