regex ^ and $

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.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-02-17 Application Insights overview
2022-02-17 Using serilog with azure application insights and .Net core
2016-02-17 How to: Synchronize Files by Using Managed Code
2016-02-17 Getting Started Synchronizing Files
2016-02-17 Introducing Microsoft Sync Framework: Sync Services for File Systems
2013-02-17 Lingo软件的使用
点击右上角即可分享
微信分享提示