I want to make PHP regex to find if text has two or more of at least one character in character set {-, l, s, i, a}. I made like this.
preg_match("/[-lisa]{2,}/", $text);
But this doesn't work. Please help me.
I want to make PHP regex to find if text has two or more of at least one character in character set {-, l, s, i, a}. I made like this.
preg_match("/[-lisa]{2,}/", $text);
But this doesn't work. Please help me.
Matching two or more occurrences means matching two is enough for the check to be valid.
At least one in character set might either mean you want to match the same char from the set or any of the chars in the set two times. If you want the former, when the same char repeats, you can use preg_match('~([-lisa]).*?\1~', $string, $match) (note the single quotes delimiting the string literal, if you use double quotes, the backreference must have double backslash), if the latter, i.e. you want to match ..l...i.., you can use preg_match('~[-lisa].*?[-lisa]~', $string, $match) or preg_match('~([-lisa]).*?(?1)~', $string, $match) (where (?1) is a regex subroutine that repeats the corresponding group pattern).
If your strings contain line breaks, do not forget to add s modifier, preg_match('~([-lisa]).*?\1~s', $string, $match).
More than that, if you want to check for consecutive character repetition, you should remove .* from the above patterns, i.e. 1) must be preg_match('~([-lisa])\1~', $string, $match) and 2) must be preg_match('~[-lisa]{2}~', $string, $match) (though, this is not what you want judging by your own feeback, so this example here is just for the record).
The ([-lisa])\1{2} pattern that you find useful matches a repeated -, l, i, s or a char three times (---, lll, sss, etc.), thus only use it if it fits your requirements.
Note that preg_match functions searches for a match anywhere inside a string and does not require a full string match (thus, no need adding .* (or ^.*, .*$) at the start and end of the pattern).
See a sample regex demo, feel free to test your strings in this environment.