-1

What i have understood about the Quantifier {}:

The first integer in Quantifier {} means at least and the second integer means at most. So {n,m} would mean at least n and at most m.

What i don't understand:

Similarly, {0,0} means at least zero and at most zero (which is equal to excluding some character). So the Regex /(?=\W{0,0})/ should exclude special characters but it doesn't why ?

Ahtisham
  • 5,216
  • 3
  • 29
  • 42
  • https://regex101.com/r/pdhMlv/1 – Tushar Dec 23 '17 at 09:22
  • 1
    It does not work that way. If you want to exclude something you can lookup `[^ ]` syntax (for single characters in a match or negative lookaheads/lookbehinds - a quantifier of {0,0} lets the assigned pattern be ignored. – Patrick Artner Dec 23 '17 at 09:22
  • 1
    Matching zero occurrences of something is not equal to excluding anything. – melpomene Dec 23 '17 at 09:25
  • What was the downvote for ? – Ahtisham Dec 23 '17 at 09:25
  • I actually have this regex: `/(?=^[a-zA-Z]+\d{0,})(?=\W{0,0})(?=^.{3,15}$)/` which i use for validating username. – Ahtisham Dec 23 '17 at 09:37
  • I don't want to permit user to write special characters. – Ahtisham Dec 23 '17 at 09:39
  • I don't see any of the two question mentioning `{0,0}`. – Ahtisham Dec 23 '17 at 10:07
  • And btw how would you know it has already been asked i don't see any of them in the suggestions before i asked the question. I always search for the suggested questions to find if they have the answer for my question. Then only i proceed to ask it. – Ahtisham Dec 23 '17 at 10:10
  • I still don't understand why my regex is not working can someone please help :( Here have a look: https://regex101.com/r/jhceiI/3 – Ahtisham Dec 23 '17 at 12:06

1 Answers1

1

Because "exactly 0 occurrences of [anything]" is satisfied by an empty string... which of course is before and after each character, making it useless for look-aheads and look-behinds

Damiano
  • 754
  • 1
  • 7
  • 19
  • Then how would i not allow user to type special character ? – Ahtisham Dec 23 '17 at 09:49
  • I would actually try to say what you want to allow. E.g. if you only want letters and numbers: `/^[a-z0-9]{3,15}$/i` looks like it could actually do what you seem to require in your reg.ex. – Damiano Dec 23 '17 at 09:52
  • I already did that before posting this question but it allowed special characters and that is why i am forcing it to not allow it. This regex `(?=^[a-zA-Z]+\d{0,})(?=^.{3,15}$)` allows special characters. – Ahtisham Dec 23 '17 at 09:57
  • Look-aheads are an advanced feature, they just complicate things in this (simple) case. The reg.ex. I provided - as far as I can understand - does what you need: https://regex101.com/r/jhceiI/1 – Damiano Dec 23 '17 at 10:06
  • If i type input `ajdks@d` for your regex `(?=^[a-zA-Z]+\d{0,})(?=^.{3,15}$)` it accepts it. Which it shouldn't because `@` is a special character – Ahtisham Dec 23 '17 at 10:25
  • I don't think you understood my regex. I want my first character to be a letter and it can have zero or more number of integer. I Update your regex accordingly but the main problem still persists. Here have a look: https://regex101.com/r/jhceiI/3 Notice it accepts special characters even when i have not specified them in the regex. – Ahtisham Dec 23 '17 at 12:02
  • 1
    You should read the explanation on regex101, because I don't think reg.ex. syntax is very clear to you. Your updated link points to a reg.ex. saying "one letter, one digit, three to 15 characters of any kind" (dot `.` means any character). You said _I want my first character to be a letter and it can have zero or more number of integer_ ... that would be `/^[a-z][0-9]*$/i` , or `/^[a-z][a-z0-9]*$/i` if you want to allow for more letters – Damiano Dec 23 '17 at 12:25
  • I figured it out before your comment but still thanks alot peace be on you. :) https://regex101.com/r/jhceiI/6 – Ahtisham Dec 23 '17 at 12:28