简体   繁体   English

dotnet中正则表达式之间的区别

[英]Difference between the Regex expressions in dotnet

what is the difference between the two regex 两种正则表达式有什么区别

new Regex(@"(([[[{""]))", RegexOptions.Compiled)

and

new Regex(@"(^[[[{""])", RegexOptions.Compiled) 

I've used the both regex but can't find the difference. 我已经使用了两个正则表达式,但找不到区别。 it's almost match similar things. 它几乎匹配类似的东西。

The regex patterns are not well written because 正则表达式模式写得不好,因为

  1. There are duplicate characters in character classes (thus redundant) 字符类中有重复的字符(因此是多余的)
  2. The first regex contains duplicate capture group on the whole pattern. 第一个正则表达式在整个模式中包含重复的捕获组。

The first regex - (([[[{""])) - matches 1 character, either a [ , a { , or a " , and captures it into Group 1 and Group 2. See demo . It is equal to 第一个正则表达式- (([[[{""])) -匹配1个字符,即[ ,a {" ,并将其捕获到组1和组2中。请参见demo

[[{"]

Demo 演示版

The second regex - (^[[[{""]) - only matches the same characters as the pattern above, but at the beginning of a string (if RegexOptions.Multiline is not set), or the beginning of a line (if that option is set). 第二个正则表达式- (^[[[{""]) -仅与上述模式匹配相同的字符,但匹配字符串的开头 (如果未设置RegexOptions.Multiline或行的开头 (如果该选项已设置)。 See demo . 参见演示 It is equal to 等于

^[[{"]

See demo 观看演示

You will access the matched characters using Regex.Match(s).Value . 您将使用Regex.Match(s).Value访问匹配的字符。

More about anchors 有关锚点的更多信息

Aslo see Caret ^: Beginning of String (or Line) 请参阅插入符号^:字符串(或行)的开头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM