tags:

views:

38

answers:

3

I have a regular expression here, and I want it to return only users, comments, and posts. However, even though I have \s, whitespace, in my negative character set, it still is extracting a match from it. Why is this?

+1  A: 

It's matching empty positions (zero-length strings), not spaces, and that's because you're using the star (zero or more) rather than the plus (one or more) repetition modifier. ([^.,()\s]+) returns only "users", "comments" and "posts".

Max Shawabkeh
+1  A: 

It seems did not match any space character, but just a position, since * will match a string with length 0. Try ([^\s().,]+)

Wm
+1  A: 

yes its because of the + Here is a powered regexp that extract each parameter $1 = user $2 = comment $3 = post

(([^.,()\s]+).(([^.,()\s]+), ([^.,()\s]+)))

but of course, cant exists comments or posts with those reserverd characters

or this its a more flexible example (([^.,()\s]+).(\s*([^.,()\s]+)\s*,\s*([^.,()\s]+)\s*))