I have a regular expression here, and I want it to return only users, comments, and posts. However, even 开发者_如何学Pythonthough I have \s
, whitespace, in my negative character set, it still is extracting a match from it. Why is this?
It seems did not match any space character, but just a position, since * will match a string with length 0.
Try ([^\s().,]+)
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".
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*))
精彩评论