Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match a pattern only if its length is bigger than a value

I have 2 strings and want to know

  1. if string 2 have a subset of the characters of string 1 on the same order

  2. this subset has a minimun size determined by me.

For example:

string 1 = stackoverflow
<br>minimun size = 5

string 2a = stack (MATCH)
string 2b = stac  (DO NOT MATCH)
string 2c = staov (MATCH)
string 2d = staZov (DO NOT MATCH)
string 2e = eflow (MATCH) 
string 2f = ewolf (DO NOT MATCH)
string 2g = somethingstacksomething (MATCH) 

I am building the regex programatically, so the first part of the problem could be solved with the expression: (s)?(t)?(a)?(c)?(k)?(o)?(v)?(e)?(r)?(f)?(l)?(o)?(w)?

But i can't figure out how to apply the "minimun character" rule. Is it possible to do it using regex?

thanks in advance!

EDIT: Added another example to complete the problem specification. Also, if you want to know, this is part of a method to evaluate the password strength specified by a user. If he defines a password derived from some other information (login, born date, name, etc) we should warn him.

like image 620
marcellorvalle Avatar asked Dec 06 '25 18:12

marcellorvalle


1 Answers

You could add a lookahead that ensures that there are five characters: (?=.{5})s?t?a?c?k?o?v?e?r?f?l?o?w?

like image 108
SLaks Avatar answered Dec 08 '25 07:12

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!