Regex for product key
I am trying to do a regex that will show all product keys with the value #####-#####-#####-#####-#####
this is the regular expressio开发者_如何学JAVAn 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.
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).
This one worked for me in Expresso:
([A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5})
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/
(([a-z0-9]{3,8}-){1,4}[a-z0-9]{3,8}(\s|,)|([A-Z0-9]{3,8}-){1,4}[A-Z0-9]{3,8}(\s|,))
That one works for me. all characters lower case or all characters upper case. paths have to be 3 to 8 characters/digits long and separated by minus. the first pattern will do for 1 to 4 repeatations, the last pattern with same structure mustn't have a minus in the end. The complete Key is followed by an comma or any white space including newLine, tab, space, nothing.
[a-z0-9]{3,8}-
= all lower characters + all digits 3 to 8 times followed by minus
([a-z0-9]{3,8}-){1,4}
= above rule 1 to 4 times
[A-Z0-9]{3,8}-
= all upper character + all digits 3 to 8 times followed by minus
|
= OR --> onyl one of the rules must be true
(\s|,)
= wollowed by any nothing, space, tab or newLine or comma
If you know, that there are only upper characters, you can delete this rule for lower chars, so you got left:
([A-Z0-9?]{3,8}-){1,4}[A-Z0-9?]{3,8}(\s|,)
This is my first post here.
Suppose you need a validation which requires a product key to be validated which is somewhat like this -
XXXX-XXXXX-XXXXXX-XXXX-XXXXX....and so on till n-th limit, where 'n' is the number of blocks(here XXXX is a block)and each block separated by a hyphen, and each block can only contain uppercase letters and digits, this could be the solution.
^[A-Z0-9]{4,8}(-[A-Z0-9]{4,8}){3,8}$
(Here each block can contain from 4 upto 8 characters, there should be minimum 3 and maximum 8 blocks.) This is ideal for Product Key validations like SAP BODS Product Key Validation.
Feel free to add your suggestion or comment.
Happy Coding, Prithwi P C
In C#, I used this and it works nicely. In my case, I'm checking a string that contains only the product key.
^(.{5}-){4}.{5}$
精彩评论