Forum guidelines
AuthorQuestion
Regular Expressions
Posted on: 10 Apr 2013
Hi,



Can you please explain me this regular expression?

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$


[a-zA-Z0-9]{8,10} means any alphabets or digits 8 to 10 characters.

but

(?!^[0-9]*$) or  (?!^[a-zA-Z]*$)  what are these expressions?
 

Thanks in advance
Anand

AuthorReply
Vulpes
  • 0
  • 0
accepted
Re: Regular Expressions
Posted on: 10 Apr 2013   Accepted Answer
Those are what are called 'negative lookahead' expressions. 

(?!exp) is the template for these where 'exp' is some regular expression.

Negative lookaheads mean that the match must not be followed by 'exp'.

A negative lookahead is like a sneak preview - after it's taken place, matching continues as though the sneak preview had not taken place.

So (?!^[0-9]*$) means first check that the text doesn't contain only digits and  (?!^[a-zA-Z]*$) means then check that it doesn't contain only letters.

Having done that ^([a-zA-Z0-9]{8,10})$ then goes back to the beginning of the text and checks that it contains between 8 and 10 letters and digits.




Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.