Help with Regex for domain names
MRAB
python at mrabarnett.plus.com
Thu Jul 30 11:56:23 EDT 2009
Feyo wrote:
> I'm trying to figure out how to write efficiently write a regex for
> domain names with a particular top level domain. Let's say, I want to
> grab all domain names with country codes .us, .au, and .de.
>
> I could create three different regexs that would work:
> regex = re.compile(r'[\w\-\.]+\.us)
> regex = re.compile(r'[\w\-\.]+\.au)
> regex = re.compile(r'[\w\-\.]+\.de)
>
> How would I write one to accommodate all three, or, better yet, to
> accommodate a list of them that I can pass into a method call? Thanks!
>
regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)')
If you have a list of country codes ["us", "au", "de"] then you can
build the regular expression from it:
regex = re.compile(r'[\w\-\.]+\.(?:%s)' % '|'.join(domains))
More information about the Python-list
mailing list