Raffaeu
Raffaeu

Reputation: 6973

Regex including one space

I need to find a regex that will match any string, at least 6 characters long, including only one space, but the space can't be the first or the last character. I tried many combinations of \S and \s but I am now stuck. Any help?

Upvotes: 1

Views: 1343

Answers (2)

Melissa
Melissa

Reputation: 1

preg_match_all("/[^\s][.*]{6,}[^\s]/", $str, $matches);

Upvotes: -1

Paul
Paul

Reputation: 141829

Including one or zero spaces:

^(?=.{6,})(\S+\s?\S+)$

If you meant that it must have a space then you should remove the ? in \s?. This will only work in a regex engine that supports positive lookaheads and recognizes the \s and \S sequences. RegexPal

Upvotes: 5

Related Questions