首页 > 其他分享 >regex ^ and $

regex ^ and $

时间:2023-02-17 14:58:09浏览次数:35  
标签:regex 15 string pattern Contains least match

Difference between ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$  vs (?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}  demonstrate the difference through examples.

The difference between the regular expressions ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$ and (?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15} is that the former includes the start-of-string ^ and end-of-string $ anchors, while the latter does not.

The regular expression ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$ matches any string that:

  • Starts with the beginning of the string (^ anchor)
  • Contains at least one lowercase letter ((?=.*[a-z]))
  • Contains at least one uppercase letter ((?=.*[A-Z]))
  • Contains at least one digit ((?=.*\d))
  • Has a length between 8 and 15 characters (.{8,15})
  • Ends with the end of the string ($ anchor)

For example, the following strings would match this pattern:

  • Abcde123
  • Pa$$word99

The regular expression (?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15} matches any string that:

  • Contains at least one lowercase letter ((?=.*[a-z]))
  • Contains at least one uppercase letter ((?=.*[A-Z]))
  • Contains at least one digit ((?=.*\d))
  • Has a length between 8 and 15 characters (.{8,15})

This regular expression does not include the start-of-string ^ and end-of-string $ anchors, so it will match any string that contains a substring that satisfies the pattern. For example, the following strings would match this pattern:

  • abcDEF123
  • ThisIsAReallyLongPassword1

Note that the second example has more than 15 characters, but it still matches the pattern because the pattern only requires that the string has a length between 8 and 15 characters, and this substring satisfies the other conditions of the pattern.

 

I am asking you that if a regex without ^ and $, then it only matches the substring instead of whose string?

In general, when a regular expression is used without the ^ and $ anchors, it can match any substring of the input string that meets the criteria specified in the pattern. For example, the pattern cat would match the substring "cat" in the input string "The cat in the hat". However, it would also match "cat" in the input string "concatenate", which may not be the intended match.

On the other hand, when the ^ and $ anchors are used, the pattern must match the entire input string from the beginning to the end. This can help to ensure that the pattern matches the intended text and does not match unintended substrings.

 

标签:regex,15,string,pattern,Contains,least,match
From: https://www.cnblogs.com/chucklu/p/17130084.html

相关文章