Solved

Powershell or Regex?

Posted on 2013-05-28
2
402 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…

623 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