EnterpriseMike
EnterpriseMike

Reputation: 195

PowerShell Regex - trying to match CamelCase tokens within a string

I have a naming convention base on the following: type-BusinessunitAppnameEnvironmentRole-location

eg: for a resource group: rg-BusunitDeployTstWorkload01-ae

The number of CamelCase tokens is constant.

I'm trying to build some regex to validate the format. My first regex:

^rg-[A-Z][a-z_0-9]{3,}[A-Z][a-z]{3}(Dev|Prd|Stg|Tst|Dr)[A-Z][a-zA-Z_0-9]{3,}(-ae|-as)$

matches successfully.

However, it also matches the failure case:

rg-BusUNITDeployTstWorkload01-ae

If I look at $Matches, I see:

Name                           Value
----                           -----
2                              -ae
1                              Tst
0                              rg-BusUNITDeployTstWorkload01-ae

I've started to add more parenthesis grouping, and tried to build up some other test strings but I don't feel like I'm getting anywhere. Eg:

"^rg-([A-Z][a-z_0-9]{3,})([A-Z][a-z]{3})(Dev|Prd|Stg|Tst|Dr)[A-Z][a-zA-Z_0-9]{3,}(-ae|-as)$"

yields

PS> $Matches
Name                           Value
----                           -----
4                              -ae
3                              Tst
2                              ploy
1                              BusUNITDe
0                              rg-BusUNITDeployTstWorkload01-ae

I don't get why that first match is a match - it looks like:

BusUNITDe -match ([A-Z][a-z_0-9]{3,}) # is true

--> which doesn't make sense to me.

Searching the web indicates that this is a typical approach for handling CamelCase, so I'm not sure if this is a PowerShell issue, or my misunderstanding. My money is on option 2, but I'd appreciate if anyone can point me in the right direction.

Thanks in advance!

Upvotes: 0

Views: 167

Answers (1)

Kamran Pervaiz
Kamran Pervaiz

Reputation: 1931

please try the following regular expression if it works. If you can give more examples then it would be easy.

rg-([A-Z][a-z_0-9]+)+[0-9]+-ae

Below is the description how I did it

rg-         to match first "rg-"
(           group start
[A-Z]       match the capital letter
[a-z_0-9]+  match lower case letters/digits (should be following capital letter)
)+          parenthesis close for group and repeat this group multiple times
[0-9]{2}    followed by 2 numbers 01
-ae         followed by -ae

we can make it more stricter by doing something like below:

rg-(([A-Z][a-z]+)+){4}[0-9]{2}-ae

it uses same principal but counts Camel case followed by some letters 4 times and digit 2 times. it depends how much you strict you want to be.

Upvotes: 1

Related Questions