Skip to main content
The 2024 Developer Survey results are live! See the results
fixed regex in linked code (remove backslashes before brackets)
Source Link
bobble bubble
  • 17.8k
  • 3
  • 29
  • 50

Very simple task: extract the region from an AWS arn.

Example:

arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

I need to extract eu-west-2

I have a working regex for this: ^(?:\[^[^:\]+]+:){3}(\[^[^:\]+]+).*

I tried this command, but it returns the entire string:

echo "arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3" | grep -oP '^(?:[^:]+:){3}([^:]+).*'

output: arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

What is wrong with the above?

Very simple task: extract the region from an AWS arn.

Example:

arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

I need to extract eu-west-2

I have a working regex for this: ^(?:\[^:\]+:){3}(\[^:\]+).*

I tried this command, but it returns the entire string:

echo "arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3" | grep -oP '^(?:[^:]+:){3}([^:]+).*'

output: arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

What is wrong with the above?

Very simple task: extract the region from an AWS arn.

Example:

arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

I need to extract eu-west-2

I have a working regex for this: ^(?:[^:]+:){3}([^:]+).*

I tried this command, but it returns the entire string:

echo "arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3" | grep -oP '^(?:[^:]+:){3}([^:]+).*'

output: arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

What is wrong with the above?

edited tags
Link
Wiktor Stribiżew
  • 621.7k
  • 39
  • 481
  • 596
Source Link
Kappacake
  • 1.9k
  • 21
  • 44

Bash - Extract region from AWS arn

Very simple task: extract the region from an AWS arn.

Example:

arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

I need to extract eu-west-2

I have a working regex for this: ^(?:\[^:\]+:){3}(\[^:\]+).*

I tried this command, but it returns the entire string:

echo "arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3" | grep -oP '^(?:[^:]+:){3}([^:]+).*'

output: arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

What is wrong with the above?