views:

229

answers:

4

Valid ones should contain at least one number or letter (from 6 to 15 chars long) in any order. e.x.

11111a

111a11

a11111

I found similar posts within SO but they seem to be out of order...

A: 

that should do it: \w{6,15} if you want to match the whole string: ^\w{6,15}$

amikazmi
This doesn't match the requirement *should contain at least one character or letter*
Philippe Leybaert
thats true.. 111111 is not valid
Chocol8
+3  A: 

This will match 6 to 15 characters (letters or digits), except all digits or all letters:

^(\p{L}|\p{N}){6,15}(?<=\p{L}.*)(?<=\p{N}.*)$

aaaaa1aaaa matches

1111111a11 matches

aaaaaaaaaa doesn't match

1111111111 doesn't match

Philippe Leybaert
Yep, that updated version also matches - well done too :)
Zhaph - Ben Duguid
Looks like this could be the one: http://regexhero.net/tester/?id=a49ea7cd-964f-4127-b5f6-b4c3a7d76515
Zhaph - Ben Duguid
I agree. This looks better than mine. I was just reading up about the \p notation here: http://www.regular-expressions.info/unicode.htmlIt looks like this is the trick to making this work.
Steve Wortham
A: 

You would need a look ahead query for that one.

You can create a regexp token that will try to find a match, but will not "consume" the input string. You can follow that one with a simple 2nd query that will validate the length of your string.

You can combine those to create the query you desire.

The syntax for the .Net Version of the regexp engine would be something like this:

This regexp is only written in this chatbox, and not tested... so have mercy :)

I am not sure whats a "Character or letter, but I will assume you mean "a-Z"

(?=.*[a-zA-Z]).{6,15}

Heiko Hatzfeld
a-zA-Z will only match the roman letters a through z. It will not match any characters like å or ö - For more info, see "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)": http://www.joelonsoftware.com/articles/Unicode.html
Zhaph - Ben Duguid
ooups! sorry about that.. i mean number or letter
Chocol8
You could use \w (lower cased) instead of a-zA-Z. This would also match unicode word characters.http://msdn.microsoft.com/en-us/library/20bw873z.aspx#WordCharacter
yodaj007
@yodaj007: note that `\w` also matches underscore characters.
Fredrik Mörk
A: 

Looks like this works:

^(?=.*[a-zA-Z].*)\w{6,15}(?<=.*\d.*)$

And here it is with test cases:

http://regexhero.net/tester/?id=83761a1e-f6ae-4660-a91f-9cdc4d69c7b3

Basically my idea was to use a positive lookahead first to ensure at least one letter is included. Then I use \w{5,16} as a simple means of ensuring that I match the correct number of characters (and that they are alphanumeric). And then at the end I use a positive lookbehind to ensure that the string includes at least one number as well.

Steve Wortham
Yep, nice one, well done - it also works with unicode, which doesn't appear in your test cases: http://regexhero.net/tester/?id=1f06c36f-41dd-4526-9c23-db1b1677ad1b
Zhaph - Ben Duguid
Well, it's looking explicitly for unicode letters and Arabic numerals in the 0-9 range - which ones are the numerals in your string there?
Zhaph - Ben Duguid
@strakastroukas -- Interesting, I'm not sure how to match that off the top of my head.
Steve Wortham
@Zhaph@TheSteveEdited: this should not fail in validation but it does "Ελληνικά1"
Chocol8