Matt Immer
Matt Immer

Reputation: 519

Troublesome regex matching

I was hoping some Regex-experts out there could help me with this one. I have some examples that I would like to match:

fooenvwesteucontainerregistry.azurecr.io
http://healthcheck-env-westeu-container.westeurope.azurecontainer.io
https://foo-env2-westeu-app-myapp.azurewebsites.net
https://foo-io9-westeu-app-myapp.azurewebsites.net
http://healthcheck-env-westeu-container.westeurope.azurecontainer.io

I would like to have a regex that matches the "env", "env2", and "io9". As I see it the "westeu" is sort of the anchor and I tried to match based on that. The closest I got was using (?<=foo)(-?)(\w+)(-?)(westeu) but it doesn't work in the instances where "foo" doesn't appear.

https://regex101.com/r/xcIfbY/1/

Thanks!

Upvotes: 1

Views: 46

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627380

You may use

(?:foo)?-?(\w+)-?westeu

See the regex demo and the Regulex graph:

enter image description here

Details

  • (?:foo)? - an optional sbubstring foo
  • -? - an optional -
  • (\w+) - Group 1: one or more letters, digits, _
  • -?westeu - an optional - and then westeu

Upvotes: 2

Related Questions