Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell or Regex?

Posted on 2013-05-28
2
Medium Priority
?
405 Views
Last Modified: 2013-05-28
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}"
0
Comment
Question by:enjama
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39201216
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.

e.g.

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

Open in new window


More information:  http://www.regular-expressions.info/anchors.html
0
 

Author Closing Comment

by:enjama
ID: 39201690
As a result of this comment, my regex went from

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

to

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

and it now works properly.  Thank you!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question