Powershell or Regex?

Hi people -

Just curious -
look at the following expression in powershell.  Why does this return true?  I believe the limit here is 3, NO?

"8888888888888888888" -match "[0-9]{0,3}"
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
Because the pattern can locate between 0 and 3 digits. You haven't bounded the pattern, so any sequence of 3 digits within the string will match. In this case, it will be the first three digits.

If you want to make certain the target string is *exactly* 3 digits, then add anchors, specifically the "start of string" and "end of string" anchors, to your pattern.


"8888888888888888888" -match "^[0-9]{0,3}$"

Open in new window

More information:  http://www.regular-expressions.info/anchors.html
enjamaAuthor Commented:
As a result of this comment, my regex went from

"888-367-1240" -match "[0-9]{3,3}-[0-9]{3}-[0-9]{4}"


"888-367-1240" -match "^[0-9]{3,3}-[0-9]{3}-[0-9]{4}$"

and it now works properly.  Thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.