开发者

Why is this regex behaving like this?

开发者 https://www.devze.com 2022-12-19 03:24 出处:网络
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 ext

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*))

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号