BlogueroConnor
BlogueroConnor

Reputation: 1943

Interpret this particular REGEX

I did a REGEX pattern some time ago and I don't remember its meaning. For me this is a write-only language :)

Here is the REGEX:

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

I need to know, in plain English, what does it means.

Upvotes: 0

Views: 578

Answers (3)

Boldewyn
Boldewyn

Reputation: 82784

Perl (and Python accordingly) says to the (?!...) part:

A zero-width negative lookahead assertion. For example /foo(?!bar)/ matches any occurrence of 'foo' that isn't followed by 'bar'. Note however that lookahead and lookbehind are NOT the same thing. You cannot use this for lookbehind.

That means,

(?!^[0-9]*$)

means: don't match, if the string contains only numbers. (^: start of line/string, $: end of line/string) The other accordingly.

Your regexp matches any string, that contains both numbers and letters, but not only one of them.

Cheers,

Update: For your future RegExp tailoring, take a look at the (?#...) pattern. It allows you to embed comments in your regexp. There is also a modifier, re.X, but I don't like this very much. It's your choice.

Upvotes: 4

Pauk
Pauk

Reputation: 2631

RegexBuddy says the following (!?!):

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

Options: ^ and $ match at line breaks

Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!^[0-9]*$)»
   Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
   Match a single character in the range between “0” and “9” «[0-9]*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Assert position at the end of a line (at the end of the string or before a line break character) «$»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!^[a-zA-Z]*$)»
   Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
   Match a single character present in the list below «[a-zA-Z]*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      A character in the range between “a” and “z” «a-z»
      A character in the range between “A” and “Z” «A-Z»
   Assert position at the end of a line (at the end of the string or before a line break character) «$»
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below and capture its match into backreference number 1 «([a-zA-Z0-9]{8,10})»
   Match a single character present in the list below «[a-zA-Z0-9]{8,10}»
      Between 8 and 10 times, as many times as possible, giving back as needed (greedy) «{8,10}»
      A character in the range between “a” and “z” «a-z»
      A character in the range between “A” and “Z” «A-Z»
      A character in the range between “0” and “9” «0-9»
Assert position at the end of a line (at the end of the string or before a line break character) «$»  

Upvotes: 2

Simeon Pilgrim
Simeon Pilgrim

Reputation: 26043

(?!^[0-9]*$)

don't match only numbers,

(?!^[a-zA-Z]*$)

don't match only letters,

^([a-zA-Z0-9]{8,10})$

match letters and number 8 to 10 characters long.

Upvotes: 5

Related Questions