question

RogerZimmerman-5813 avatar image
0 Votes"
RogerZimmerman-5813 asked IanXue-MSFT commented

PowerShell REGEX quandry

I'm totally lost on this regex result on PowerShell.
PS C:\Windows\system32> "917021" -match "[0-9]{4}"
True
Also tried this:
PS C:\Windows\system32> "917021" -match "[0-9]{4,5}"
True
According to all the rules in the book, this should limit the string to four characters and in the second case to five characters at the most. In fact, as I read it this is the very reason for the curly braces. Yet PowerShell passes both versions. Can anyone explain this behavior?

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
Has your problem been solved? Please feel free to let us know if more assistance is needed.
If the reply is helpful, please “Accept Answer” to help other community members find it more easily.

0 Votes 0 ·
MotoX80 avatar image
0 Votes"
MotoX80 answered

You just need to examine the $matches variable. Or $matches[0].

103202-capture.jpg



capture.jpg (26.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RogerZimmerman-5813 avatar image
0 Votes"
RogerZimmerman-5813 answered RichMatheisen-8856 edited

That makes some sense to me, except it seems deceptive that it would return a TRUE if it isn't a complete match, when the whole point of the { } is to limit the set of characters to only 4 (or 5). It seems that the whole point of regex is to filter out mismatches. Let's say I wanted to only send something to servers named 9070 and 90702, but not 907024 and 907025. The script sees that the partial match and returns TRUE, defeating the point of the { }. Doesn't that seem wrong somehow? Forgive me if I'm being a bit dense on this.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You're confusing a pattern match with a string comparison.

The pattern [0-9]{4} will return TRUE if you test a string like "7902", "A9070B", or "190702" because the pattern of four consecutive digits was found.



0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

[0-9]{4} matches 9170 and [0-9]{4,5} matches 91702 so they both return TRUE. To retrieve captured text you could use the $Matches hashtable automatic variable

103203-image.png


Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (7.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RogerZimmerman-5813 avatar image
0 Votes"
RogerZimmerman-5813 answered

Thanks all for your input. This is going to take some work, I can see. i am thankful to the community for your patience and help.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.