Python Forum
Hide symbol or characters in regular expression - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Hide symbol or characters in regular expression (/thread-25138.html)



Hide symbol or characters in regular expression - Gateux - Mar-21-2020

I'm trying to find a full list of domain names from a imported text file and the correct regular expression used is this

import re
hand = open('mbox-short.txt')
for line in hand:
    line = line.rstrip()
    x = re.findall('[a-zA-Z0-9]\S*@(\S*[a-zA-Z0-9])', line)
    if len(x) > 0:
        print(x)
However the regular expression created is way too long, so i came up with this:

x = re.findall('@\S+', line)
By doing the above, i'm able to get the same exact as the longer regular expressions, however because I specify the @ in front, the results printed @ on every line too. So i'm trying to figure out if there is a way or code that I can use to hide the @ symbol in the result? Reason being it will save me the trouble of writing the longer regular expression as it's a pain in the neck.