I'm converting some php code to python. The php code has the following for matching a surname from a string:
preg_match("/([^ ]*) *$/",$c['name'],$r);
which appears to just match whatever the last word in the string is.
Looking to convert it, I thought the following would do it:
r = re.match('([\S]+)$', c['name'])
to my mind that should match any non-space characters before the end of the string. The logic is that $
matches the end of the string, and the ([\S]+)
matches any non space character one or more times before the end of the string. However that doesn't work for me (despite a site like pythex.org suggesting it should).
I've managed to get it to work using the following, but I'd like to know why the above regex doesn't work, I suspect it has to do with back-capture but I'm not really familiar with how that works.For testing I'm just using c['name'] = 'John Doe'
r = re.match('(?:.*\s)*([\S]+)', c['name'])
(in the above regex the (?:.*\s)
is a non-capture group that matches repeated characters followed by a space-like character 0 or more times. It then captures any non-space-like characters that occur one or more times.)
Thanks.