简体   繁体   English

“[\S\s]*?” 这个正则表达式是什么意思?

[英]"[\S\s]*?" What Does This Regex Mean?

As far as I know,据我所知,

  • [\S\s] == Match Any Character, [\S\s] == 匹配任何字符,
  • * == match any character * == 匹配任意字符
  • ? == optional. == 可选。

What does this means?这是什么意思?

System.Text.RegularExpressions.Regex.Match(getMsg,"(?<=Status         :\s+)[\S\s]*?(?=  ╙█)").Value

The input that the regex is used on is:使用正则表达式的输入是:

"Status : ******** "

You are correct on the first assessment - [\S\s] will match any character - INCLUDING whitespaces and linebreaks.您在第一次评估时是正确的 - [\S\s] 将匹配任何字符 - 包括空格和换行符。 The second assessment isn't technically correct - the asterisk wildcard symbol means that it will match zero or more of the preceding token, so in combination with the first part, yes will mean 'match any character'.第二个评估在技术上是不正确的——星号通配符意味着它将匹配零个或多个前面的标记,所以结合第一部分,yes 将意味着“匹配任何字符”。

? in RegEx does not mean 'optional'.在 RegEx 中并不意味着“可选”。 It means: Makes the preceding quantifier lazy, causing it to match as few characters as possible. By default, quantifiers are greedy, and will match as many characters as possible.这意味着: Makes the preceding quantifier lazy, causing it to match as few characters as possible. By default, quantifiers are greedy, and will match as many characters as possible. Makes the preceding quantifier lazy, causing it to match as few characters as possible. By default, quantifiers are greedy, and will match as many characters as possible. (Source: https://regexr.com/ ). (来源: https ://regexr.com/)。 This essentially will negate the asterisk quantifier and will mostly cause unexpected results - use '*' and '?'这基本上会否定星号量词,并且主要会导致意外结果 - 使用“*”和“?” together with caution.一起谨慎。

Along with the preceding \s+ token used - there is a little too much going on in this RegEx.连同前面使用的 \s+ 标记 - 这个正则表达式中发生的事情太多了。 Check out https://regexr.com/ and use this as an always tool when employing RegEx.查看https://regexr.com/并在使用 RegEx 时将其作为常用工具。 This site not only explains every aspect of a RegEx, but also lets you test your current expression to see if a test expression matches.该站点不仅解释了 RegEx 的各个方面,还允许您测试当前表达式以查看测试表达式是否匹配。

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

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