0

I have been trying learning regex for this task. I have used lookahead to find the three conditions.

  1. Check for lowercase (?=.*[a-z])
  2. Check for uppercase (?=.*[A-Z])
  3. Check for numeric (?=.*[0-9])

This case work fine. When I combine I get

(?=.\*\d)(?=.\*[a-z])(?=.*[A-Z])

This return true for any string containing number, a-z and A-Z. But I need only regex containing alpha numeric. I tried this regex

^(?=.\*\d)(?=.\*[a-z])(?=.*[A-Z])$

But this does not work. Can anyone help on why does this regex not work for the combination.

Edit: My doubt is why the ^ and $ do not work there when I combine

CodeFranc
  • 3
  • 3
  • I checked it. i dont understand why my regex did not work. there is no explanation there – CodeFranc May 07 '19 at 18:39
  • Lookarounds just look for matches but actually doesn't consume any characters. Your regex is just missing the part that would actually consume the characters, which is `.{8,}` that you need to place just before `$` and lets say your password at least needs to have 8 characters. – Pushpesh Kumar Rajwanshi May 07 '19 at 18:40
  • 1
    You shouldn't restrict passwords from containing non-alphanumeric characters. That will only make them less secure. And there is virtually never a good reason to do so. – Ivar May 07 '19 at 18:41
  • Sory I had missed that part – CodeFranc May 07 '19 at 18:43

1 Answers1

1

The proper regex here would be

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]*$

You need to add the actual text matching part.

Lookaheads are used to just check if a something is followed by something else without making it an actual part of the match. So, in order to make it work you need to specify the actual match part of the regex. That part would be [a-zA-Z0-9]*

BlackPearl
  • 1,687
  • 1
  • 7
  • 16