[最佳答案] Note that your pn = re.compile(r'@(\S+)') regex will capture any 1+ non-whitespace characters after @. To exclude matching :, you need to convert the shorthand \S class to [^\s] negated character class equivalent, and add : to it: pn = re.compile(r'@([^\s:]+)') Now, it will stop capturing non-whitespace symbols before the first :. See the regex demo. If you need to capture until the last :, you can just add : after the capturing group: pn = re.compile(r'@(\S+):'). As for a URL matching regex, t