• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 946
  • Last Modified:

foxpro - finding domain name within URL with Regular Expressions

Yesterday pcelba posted the exact code that I needed to find all email addresses in text:

www.experts-exchange.com/Microsoft/Applications/FoxPro/Q_28284572.html (see last reply in solution)

I'm going to use the same code for extracting the domain name from a URL, but don't know what regular expression pattern that I need.

from "www.google.com"
to extract 'google'


from "https://secure.experts-exchange.com""
to extract "experts-exchange"

I've tried various patterns found here:

but they don't seem to work in Foxpro with the code in the answer from yesterday.
Does anyone have a tried and tested pattern that works?
3 Solutions
If you have URL already then you don't need RegExp to extract the domain name BUT you have to define what "domain" means for you... (It is stated in the StackOverflow already)

www.google.com ... google
www.celba.cz  ... celba
celba.cz  ... celba

that was easy... but

www.abc.co.uk ... co   or   abc.co   or   abc ?

And what about visualfoxpro.application etc ?

So you'll need to distinguish between domains and e.g. OLE class names as the above one and also other similar elements... and their names can be very similar if not equal to domains.

To define a list of TLDs or domain suffixes is good idea probably.

And to do the whole task you just need STREXTRACT() or AT() and SUBSTR() functions in FoxPro.
Olaf DoschkeSoftware DeveloperCommented:
Pavel has got the main point, there are much more top level domains and more are coming. I don't see that as a task for regexp anymore, too complicated.

Looking at http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax
What you want to extract is only the second level domain part of the host name without the top level domain.

The same second level domain combined with other top level domains may be interesting for domain grabbers or researches for a potential product name, etc.

So there is use for extracting that info, but another top level domain normally means another hoster, maintainer, owner, service, brand, market, etc. Only the full hostname SLD.TLD is techncally relevant and stands for an IP (or a series of IPs related to the same host). Being able to extract or stripe off a subdomain is perhaps more important.

I'd let DNS do that for me. Extract the part between : and the first following / and see what IP you get from DNS, repeat with partial names. The shortest name giving you the same IP from DNS is the hostname, and the last part(s) of that is/are a TLD.

Bye, Olaf.
Derek JensenCommented:
The most common/useful regexp I usually use for locating a URL goes something like this:


Open in new window

But as for grabbing the rest of the URL, I'll have to defer to prior experts' comments. :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now