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?
views:
38answers:
3
+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
2010-02-06 05:41:25
+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
2010-02-06 05:42:23
+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*))