frosty
frosty

Reputation: 2649

Matching all characters except spaces in regex

Right now I have a regex, and I want to change one part of the regex.

(.{3,}?) ~

^---This part of the code, where it says, (any characters that are 3 or more in length, and matches up to the nearest space), I want to change it to (any characters, except spaces , that are 3 or more in length, and matches up to the nearest space). How would I say that in regex?

$text = "my name is to habert";
$regex = "~(?:my name is |my name\\\'s |i am |i\\\'m |it is |it\\\'s |call me )?(.{3,}?) ~i";
preg_match($regex, $text, $match);
print_r($match);

Result:

Array ( [0] => my name [1] => my name )

Need Result:

Array ( [0] => name [1] => name )

Upvotes: 0

Views: 7113

Answers (1)

Cheloide
Cheloide

Reputation: 805

Gravedigger here... Since this question does not have an answer yet, I'll post mine.


(\S{3,}) will work for your needs


Regex Explanation:

( Open capture group

\S Everything but whitespaces (same as [^\s], you can use [^ ] too, but the latter works only for spaces.)

{3,} Must contain three or more characters

) Close capture group

Test it here!

Upvotes: 1

Related Questions