Solved

Powershell or Regex?

Posted on 2013-05-28
2
331 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
2 Comments
 
LVL 74

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now