I need to look at a string, and find - character that is used more than 1 time
$input = "stack-overflow - 2014 - new" //Contains repeated character - more than 2 times
I found all sorts of really close answers already, but not quite. Is there a PHP regular expression to catch this case? (I have a basic regexp knowledge but this is too much for me)
$regex=' '; //// that what i need
if (preg_match('/(\w)\1{5,}/', $input)) {
# Successful match
} else {
# Match attempt failed
}
Regex is not the best choice to count the occurrence in a string. For PHP, there exists a method called substr_count()
e.g.
$s = 'stack-overflow - 2014 - new';
if (substr_count($s, '-') >= 2) {
// at least 2 times
}
I think what you're asking is how to match multiple hyphen characters (-
) with RegEx? If so, this expression will do the trick:
/-+/
/
= delimiter
-
= match the hyphen character
+
= match multiple occurrences of the preceding character