tags:

views:

212

answers:

5

an 20 - 24 char long alphanumeric string with no spaces and no symbols that has at least 2 digits

AAAAAAAAAAAAAAAAAAAA - not valid
AAAAAA0AAAAAAAAA0AAA - valid
AAAAAA01AAAAAAAAA0AAA - valid
AAAAAA0AAAAAAAAA0AAA@ - not valid
+5  A: 

I think this is only possible with look-ahead assertion:

^(?=[a-zA-Z\d]{20,24}$)[a-zA-Z]*\d[a-zA-Z]*\d[a-zA-Z\d]*$

The look-ahead assertion ((?=[a-zA-Z\d]{20,24}$)) checks if the string has the expected form (20–24 alphanumeric characters). And the second part ([a-zA-Z]*\d[a-zA-Z]*\d[a-zA-Z\d]*) checks if it contains at least two digits.

Gumbo
Note that that won't work unless the pattern is the entire string / line.
MarkusQ
Also, let's try to make people work for their homework; in a few years they'll be our co-workers and we're all better off if they've had to think hard at least a half dozen times before graduation.
MarkusQ
hmm it doesn't work, im using c# with visual studio@MarkusQ it's not homework, i need to use it to find some old files
nevermind i got it to work thanks
+1  A: 

I'm going to be abstract because this sounds like homework (if it is, please tag it as such).

  • You can restrict the number of times a pattern matches with {min,max}
  • You can restrict which characters match with [charlist]
  • You can impose additional restrictions with what's called zero-width positive lookahead (there's also a negative form). The syntax varies, so check the docs for your environment.

Update your question (& tags) if you need more help.

MarkusQ
it's not homework, im just new to regular expressions
@sol -- Then see Gumbo's answer. It helps to explain a little context to avoid setting off people's "homework detectors"
MarkusQ
sorry im new to this website but thanks
A: 

Basically the same idea as Gumbo just a little shorter:

^(?=[\w\d]{20,24}$)[\w]*\d[\w]*\d[\w\d]*$
Luis Armando
But it’s incorrect. `\w` is a shorthand for `[A-Za-z0-9_]`.
Gumbo
Also, \w can - in some regex flavours - include accented characters too.
Peter Boughton
Accented characters aren't alphanumeric?
Svante
Yes, and "\w" doesn't equal "alphanumeric", it means "word character", and accented characters *do* appear in words.See http://www.regular-expressions.info/charclass.html#shorthand for more information.
Peter Boughton
Accented characters are probably irrelevant, but \w also matches the underscore, which is not alphanumeric. Also, each \w* will initially match all the way to the end of the string, only to have to backtrack so the next \d can do its job. Why take that performance hit when it's so easy to avoid?
Alan Moore
And in Perl 5.8 and 5.10 \d matches all UNICODE characters that are digits, so U+1815 (MONGOLIAN DIGIT 5) will match.
Chas. Owens
+1  A: 

Gumbo has a correct expression for the requirements.

It could be shortened, but his was more clear and probably faster than the short version.

var rX=/^(?=[a-zA-Z\d]{20,24}$)([a-zA-Z]*\d){2,}/

kennebec
Your expression requires the string to end with a digit.
Gumbo
No it doesn't. Absent the '$' anchor, it merely stops matching once it finds the second digit. The lookahead has already verified the length (20-24) and composition (letters or digits).
Alan Moore
+2  A: 

I think that this is the simplest pattern: First make a positive lookahead to check that there are at least two digits, then match 20-24 alphanumeric characters:

^(?=.*\d.*\d)[A-Za-z\d]{20,24}$
Guffa