0

I'm trying to extract the resource group name in Azure via az cli.

The full path to a certain resource group looks like this:

/subscriptions/b049-1234-1256-125456-125/resourceGroups/Test_ResourceGroup

I'm trying to only extract "Test_ResourceGroup" out of the full string (which is stored in a variable), so i think the code would be something like

$scope = /subscriptions/b049-1234-1256-125456-125/resourceGroups/Test_ResourceGroup
$resourcegroup = $scope -match 'regex'

But I'm terrible at regex and not great at it. The addition challenge is that sometimes there's more strings or integers after the resource name, e.g.

/subscriptions/b049-1234-1256-125456-125/resourceGroups/Test_ResourceGroup/specificnameofresource/blahblah

But again, I just want the resource group name.

Ryvik
  • 363
  • 1
  • 3
  • 14
  • how do you decide what is the resource group name in the 2nd string? – Lee_Dailey May 11 '20 at 03:02
  • I expect you mean what to extract the string following the literal "/resourceGroups/" up to the next forward slash or the end of the line or string. Correct? I believe Powershell supports lookaheads and (fixed-length) lookbehinds, in which you could match `(?<=\/resourceGroups\/)[^\/]+`. [Demo](https://regex101.com/r/OT7jaK/2/). `(?<=\/resourceGroups\/)` is a *positive lookbehind*, which must be matched but is not part of the match returned. `[^\/]+` gobbles up characters other than forward slashes until it reaches a forward slash or the end of the string. – Cary Swoveland May 11 '20 at 03:12
  • Correct, the resource group name is after /resourceGroups/ – Ryvik May 11 '20 at 03:25

2 Answers2

0

while a pure regex solution is likely faster, this is simpler & easier to understand/modify. well, it is for me. [grin]

the neat thing is that the final .Split() will silently fail, so it works for "end of string" and for any position in the string.

$TestList = @(
    '/subscriptions/b049-1234-1256-125456-125/resourceGroups/Test_ResourceGroup/specificnameofresource/blahblah'
    '/subscriptions/b049-1234-1256-125456-125/resourceGroups/ResourceGroupZiggity'
    )

foreach ($TL_Item in $TestList)
    {
    ($TL_Item -split 'resourcegroups/')[-1].Split('/')[0]
    }

output ...

Test_ResourceGroup
ResourceGroupZiggity
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
0

You can use the regex-based -replace operator:

$resourcegroup = $scope -replace '.+/resourceGroups/([^/]+).*', '$1'
  • .+/resourceGroups/ captures one or more (+) characters (.), followed by /resourceGroups/, i.e. everything up to and including /resourceGroups/.

  • [^/]+ captures one or more characters that are not (^) in the character set ([...]) comprising /, i.e. everything up, but not including the next /, if any.

  • (...) is a capturing subexpression a so-called capture group), whose captured text can be referred to in the replacement operand (substitution text) as $1.

  • .* matches zero or more (*) remaining characters, i.e. whatever characters are left.

  • Since the regex matches the entire input string, replacing what it matched with '$1 in effect extracts just the token of interest.

mklement0
  • 382,024
  • 64
  • 607
  • 775