Home > database >  How do I get the regex to return more accurately?
How do I get the regex to return more accurately?

Time:11-16

I am trying to get the regex right to return one or the other but not both: When I run for example the following:

aws secretsmanager list-secrets | jq -r ".SecretList[] | select(.Name|match(\"example-*\")) | .Name "

it returns

example-secret_key

as well as

examplecompany-secret_key

how can I modify the command to return one and not the other? Thanks

CodePudding user response:

example-* matches strings that contain example followed by zero or more -.

^example- matches strings that start with example-.

jq -r '.SecretList[].Name | select( test( "^example-" ) )'

CodePudding user response:

A regular expression is not a shell glob/wildcard. * in a regex does not mean "anything", but rather "whatever came before is repeated 0 or more times". . is a single arbitrary character. .* are 0 or more arbitrary characters.

If you want to match "example-" and don't care what comes after, simply use the regex example-. If you want to match "example-", then anything or nothing, then "_key", use the regex example-.*_key.

jq -r '.SecretList[].Name | select(test("example-"))'
  • Related