0

I entered the following regex:

(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@#$%]).{8,20}

And give the following input string for pattern search:

A00123456789123456789gj673%

It gave me the matched output as:

A0012345678912345678

Can you explain me why it's giving me output when the order of characters entered in input string doesn't matched the order in which regex pattern is entered?

If possible, plz explain me how this works sequentially.

mp3001
  • 29
  • 4

1 Answers1

0

Your pattern is being partially matched. If you want to prevent it's matching, you can do it by adding ^ and $ to the begin and end of your pattern.

^: asserts position at start of a line

$: asserts position at the end of a line

The final pattern would be like:

(?=^[a-z]*[A-Z]*[0-9]*[@#$%]*$)(?=(.{8,20}))
FMoosavi
  • 150
  • 1
  • 11
  • OP asked for an explanation, not a modification. This does not answer the question. – Turing85 Jul 01 '21 at 21:47
  • @Turing85 Ok i'll explain it – FMoosavi Jul 01 '21 at 21:51
  • Thanks for the reply but it still matches for string length between 8 and 20 with any order , like if entered input string ``A012345gj673%``, it still matches. Shouldn't it be in order like after encountering a [a-z] character (eg. after encountering ```g`` or `j`` in above input string) next it must search for a [A-Z] character? The only characters after that are ``6``, ``7``, ``3`` and ``%``, there is no capital letter character. – mp3001 Jul 01 '21 at 22:08
  • @mp3001 got a match for youi. and edited the answer. Good luck – FMoosavi Jul 01 '21 at 22:37