user3566681
user3566681

Reputation: 23

Matching string with regex

I keep banging my head against the wall looking for a regex that matches a string like any of these:

--7928ae02-A--
--7928ae02-B--
--7928ae02-F--
--7928ae02-H--
--7928ae02-Z--

the string is two dashes, 8 characters of any letter or number, a dash, an uppercase A-Z and two dashes.

Here's any example of where I'm at:

grep '^--[a-fA-F0-9]{8}-[A-Z]--$'

Upvotes: 2

Views: 77

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476534

You probably should use the

grep -P '^--[a-fA-F0-9]{8}-[A-Z]--$'

with the -P or -E flag. -P interprets the regex as a Perl regex, -E runs the regex in extended mode. This regex allows all kinds of bells and whistles Like the {}-operator.

Running this on your given tests, they all pass.

Upvotes: 0

iruvar
iruvar

Reputation: 23364

This might work

grep -E -- '^--[[:alnum:]]{8}-[[:upper:]]--$'

Upvotes: 2

Related Questions