tags:

views:

114

answers:

4

I need a regular expression for which:

the string is alphanumeric and have exactly 6 characters in the first half followed by hyphen(optional) followed by optional 4 characters:(cannot have more than 4 characters in the second half)

so any of the following is valid

11111A
111111-1
111111-yy
yyyyy-989
yyyyyy-9090

i thought this expression /[a-zA-Z0-9]([-])?[a-zA-Z0-9]{5,10}$/; should work but i m unable to get it working correctly.

Any help will be appreciated,

+5  A: 
^[a-zA-Z0-9]{6}(-[A-Za-z0-9]{1,4})?$
tanascius
I think you meant `{1,4}`
Ahmad Mageed
@Ahmad: thanks, you are right
tanascius
@tanascius: np, and +1 :)
Ahmad Mageed
i tried in notepad++ and this doesnt work for 123456-1
@tanascius: \w isn't synonym of [a-zA-Z0-9], it might include other characters like underscore _
PhiLho
@user268375: Notepad++ seems to use Scintilla regex engine which is very primitive, it doesn't understand {n} for example.
PhiLho
@PhiLho, forgot about the `_` ...
tanascius
A: 

If your first 6 and last 4 are alphanumeric only:

^\w{6}-?\w{0,4}$

Note: \w is a handy shortcut for [a-zA-Z0-9_], but it allows underscores (_). If you don't want those, just replace it with [a-zA-Z0-9].

UPDATE

Two way of avoiding zeros:

  1. Spliting your group, if you know that the last figure (for example) must not be 0
    ^\w{5}[a-zA-Z1-9]-?\w{0,3}[a-zA-Z1-9]$
  2. Negative look-ahead, very powerful but not available in all regex "flavours" (implementation):
    ^(?!0{6})\w{6}-?(?!0+$)\w{0,4}$
    Those "zero-width" assertion don't consume characters, they only check what's ahead and cause the match to fail if the pattern is encountered (in the case of a negative look-ahead). The first one is easy enough, checking if we have exactly six 0s. The second one checks if there is one or more (+) 0s immediately before the end ($) of the line/string.
streetpc
they can be only digits or can be alphanumberic too
alphanumeric means letters or digits. `\w` is the same as `[a-zA-Z0-9_]`
streetpc
what if i want to add another condition stating that the first half or/and second half cannot have all zeros?Thanks for all the help
answered to that in the update
streetpc
+1  A: 
^[a-zA-Z0-9]{6}-?[a-zA-Z0-9]{0,4}$

This isn't as concise as some of the others, but it is bulletproof. Tanascius' and Streetpc's solutions will match ______-____, since \w matches underscores.

One question: you say the dash is optional and the following four characters are optional, so does that mean 111111AAAA should match? If so, my regex above is for you, since Tanascius' will not match this.

If this isn't the case, then you'll want this:

^[a-zA-Z0-9]{6}(?:-[a-zA-Z0-9]{1,4})?$

Which makes use of slightly more efficient backreferenceless grouping.

One last thing. This solution won't match 111111- but will match 11111-A. If you would like to match on 111111-, but also don't want to match 111111A, go with this:

^[a-zA-Z0-9]{6}(?:-[a-zA-Z0-9]{0,4})?$

EDIT: Tanascius' solution no longer uses \w, so disregard what's said above concerning this.

David Foster
Yeah, I am learning :) +1 for the *optional dash*, that is indeed not specified correctly
tanascius
A: 

Might be off-topic, but since I am no regex expert at all I often use the txt2regex program which helps building them from textual menus. http://txt2regex.sourceforge.net.

Cannot resist to quote this: "A programmer had a problem. He realised he could solve it using a regular expression. Now he has two problems."

Miguel Garcia-Lopez