1

I wish to match a url string with my regex pattern: r'.+'. I expect this pattern to fully match the url string, but that's not happening. I'm getting the following output:

Output:

url string: https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent
matched string: <re.Match object; span=(0, 85), match='https://stackoverflow.com/users/signup?ssrc=head&>

Clearly, the match leaves out a chunk of the url string by the end. My pattern doesn't match the returnurl=%2fusers%2fstory%2fcurrent part of the url string. Why is this happening? Should I be using something other than r'.+' for my pattern?

Code:

def getQueryParameters(url):
    print(f"url string: {url}")
    pattern = re.compile(r'.+')
    match = pattern.search(url)
    print(f'matched string: {match}')

if __name__ == '__main__':
    url = "https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent"
    getQueryParameters(url)
0

1 Answer 1

3

Printing the Match Object itself doesn't display the whole match. If you print match.group(0) instead of match, you will see your regex does match the whole string.

Sign up to request clarification or add additional context in comments.

1 Comment

Notice that there is no closing quote mark on the match='... part of the match object's representation. This is the re module's way of indicating that the string is incomplete, while still limiting the representation's length so it's not too annoying when you print it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.