Bevan
Bevan

Reputation: 1424

How do I regex match these 3 scenarios?

I have a lot of automations running some Azure deployment commands and I need to support values wrapped with quotes and none at all. It needs to support lookahead and lookbehind. It's running in Powershell so is supported.

I can extract examples 1 and 2 OK but not the 3rd without running a condition and separate regex after every capture. How would I match this in the one query?

Current Regex : https://regex101.com/r/qgX6aQ/1 : (?<= --resource-group ['"])(.*?)(?=['" ])

In this example I need to extract RGName :

  1. az vm create --resource-group "RGName" --name 'VMName'
  2. az vm create --resource-group 'RGName' --name VMName
  3. az vm create --resource-group RGName --name "VMName"

Upvotes: 0

Views: 134

Answers (3)

The fourth bird
The fourth bird

Reputation: 163207

To get the match you don't need a lookbehind, you can just match it.

You could use a named capture group, for example value. To match the opening single or double quote with a closing single or double quote, you can also use a capturing group with a backreference \1

The pattern either matches from an opening till closing single or double quote, or matches any char except a whitespace char or single and double quote.

The space at the end is also not mandatory.

--resource-group (?:(["'])(?<value>.*?)\1|(?<value>[^\s"']+))

Explanation

  • --resource-group Match literally
  • (?: Non capture group
    • (["']) Capture group 1, match either " or '
    • (?<value>.*?) Named group value, match any char except a newline, as least chars as possible
    • \1 Backreference to group 1, match the previously matched " or '
    • | Or
    • (?<value>[^\s"']+) Named group value, match 1+ occurrences of any char except a whitespace or " or '
  • ) Close non capture group

Regex demo

enter image description here

Upvotes: 1

Bevan
Bevan

Reputation: 1424

This has been in the back of my mind for a long time and most things I've tried produce the same error "A quantifier inside a lookbehind makes it non-fixed width".

Moving the character match outside of the lookbehind looks like it works :

(?<= --resource-group )['"]?(.*?)(?=['" ])

I'm still happy to take suggestions and improvements on this.

Upvotes: 0

Steve Bennett
Steve Bennett

Reputation: 126025

I'd handle the three cases separately:

(?<= --resource-group (
  '(.*?)(?=')
  |
  "(.*?)(?=")
  |
  (\w*)
  )
)


  

Upvotes: 0

Related Questions