tags:

views:

84

answers:

3

I am trying to do a regex that will show all product keys with the value #####-#####-#####-#####-#####

this is the regular expression i have created

[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}

for some reason it just isn't working.

+1  A: 

You should anchor the pattern:

/\A[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}\z/
Sinan Ünür
Why mix `^` and `\z`? Besides, it's unlikely to cause the the regex to fail.
zneak
No reason except that, in the absense of `/m`, `^` and `\A` mean the same thing whereas `$` and `\z` don't. Without anchoring, the pattern might match even when there are extra characters in the string and that might be what the OP means by *failing*. That was my best guess.
Sinan Ünür
+1  A: 

In what language or system are you doing this? Not all of them accept the {n} repetition notation.

Hyman Rosen
A: 

Which regex tool do you intend to use? grep, egrep, sed, perl, etc?

Also, you may want to allow lowercase letters:

Using egrep, and cygwin, this works: '^([A-Za-z0-9]{5}-){4}[A-Za-z0-9]{5}$'

But as Hyman points out, {} is not valid in every regex set (hence why I used egrep, not grep).

Tim