What does .* do in regex?

前端 未结 3 1524
余生分开走
余生分开走 2021-02-10 06:52

After extensive search, I am unable to find an explanation for the need to use .* in regex. For example, MSDN suggests a password regex of

@\\\"(?=.{6,})(?=(.*\\         


        
3条回答
  •  醉话见心
    2021-02-10 07:32

    .* just means "0 or more of any character"

    It's broken down into two parts:

    • . - a "dot" indicates any character
    • * - means "0 or more instances of the preceding regex token"

    In your example above, this is important, since they want to force the password to contain a special character and a number, while still allowing all other characters. If you used \d instead of .*, for example, then that would restrict that portion of the regex to only match decimal characters (\d is shorthand for [0-9], meaning any decimal). Similarly, \W instead of .*\W would cause that portion to only match non-word characters.

    A good reference containing many of these tokens for .NET can be found on the MSDN here: Regular Expression Language - Quick Reference

    Also, if you're really looking to delve into regex, take a look at http://www.regular-expressions.info/. While it can sometimes be difficult to find what you're looking for on that site, it's one of the most complete and begginner-friendly regex references I've seen online.

提交回复
热议问题