How to ignore words equals in string with regex?

I am using the following regexp to capture the data string.

preg_match_all('/#(\\w+)/', $string, $matches);

Ex:

#car #CAR

He should ignore the second.


preg_match_all('/#([^#]+)/', $string, $matches);

That should do it. Or if the only characters you want to match after the first # are letters:


preg_match_all('/#([a-zA-Z]+)/', $string, $matches);

Works! Thank you!! =D

Sorry for the double post, but I still have a problem:

How do I do to remove the space from the end of each match?

If I try to use ‘\s’ the above example does not work …

This in preg_replace();

The second one will do that. Maybe digits should also be OK, then:


preg_match_all('/#([a-zA-Z0-9]+)/', $string, $matches);

or amending the first one:


preg_match_all('/#([^# ]+)/', $string, $matches);

In fact, if a space always follows what you want to capture, then this will do:


preg_match_all('/#([^ ]+)/', $string, $matches);

Thank you again!! =D