1
votes

I want to define a perl regex that will satisfy below conditions

  1. Minimum length is 8 and maximum is 24. - (^[\w!#+,-./:=@]{8,24}$)
  2. Same character can not appear consecutively 8 or more times - ([\w!#+,-./:=@])\1{7}

  3. First character can not be special one - ^[^a-zA-Z0-9]+

  4. Character allowed - \w!#+,-./:=@

I am able to achieve this separately, but how to combine all these 3 regex .

Thanks in advance.

1

1 Answers

3
votes

Try this regex:

^(?!.*(.)\1{7})[A-Za-z0-9][\w!#+,./:=@-]{7,23}$

Here is an explanation:

^                          from the start of the string
    (?!.*(.)\1{7})         assert that the same character does not occur 8 or more
                           times in a row
    [A-Za-z0-9]            match an inital non special character
    [\w!#+,./:=@-]{7,23}$  then match 7 to 23 of any character
$                          end of input

Demo