0
votes

I'm trying to do a simple regular expression in Python3 that just provides me the Twitter id.

Why does the following not work?

import re
s="twitter.com/atechnofou\\nInstagram:"
all_matches = re.findall("twitter\.com/[^?\\\"\'>&\*\n\r\ <]+", s)

It returns: ['twitter.com/atechnofou\nInstagram:']

But I'd like it to return ['twitter.com/atechnofou']


If I change my re.findall to this, it works, but why?

all_matches = re.findall("twitter\.com/[^?\"\'>&\*\n\r\\\ <]+", s)

All I've done is moved the \\ from the beginning of the [] block to closer to the end.

\\n is two characters: a literal backslash and the letter n, not a new line. - Mark Tolonen
But shouldn't the \\ still catch it? - Gary
Maybe need a raw string? Not where I can try it right now - Mark Tolonen