Help with Regex for domain names

rurpy at yahoo.com rurpy at yahoo.com
Thu Jul 30 13:29:09 EDT 2009


On Jul 30, 9:56 am, MRAB <pyt... at mrabarnett.plus.com> wrote:
> 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)')

You might also want to consider that some country
codes such as "co" for Columbia might match more than
you want, for example:

  re.match(r'[\w\-\.]+\.(?:us|au|de|co)', 'foo.boo.com')

will match.



More information about the Python-list mailing list