Extract domain name using Java regular expression
|
|
|
Extract domain name using Java regular expressions
In this sample we are using Java regular expressions to extract domain names.
Java method to extract domains
Let's define the regular expression pattern :
[a-z0-9\\-\\.]+\\.(com|org|net|mil|edu|(co\\.[a-z].))
| Pattern | Description | Reference | ||
|---|---|---|---|---|
| [a-z0-9\\-\\.]+ | one or more times a group of number,letter or hyphen | |||
| \\. | ||||
| ( | Start of a group #1 | |||
| com | .com domain | |||
| | | ||||
| org |
.org domain |
|||
| | | ||||
| net |
.net domain |
|||
| | | ||||
| mil | .mil domain | |||
| | | ||||
| edu | .edu domain | |||
| | | ||||
| ( |
Start of a group #1.1 |
|||
| co\\.[a-z]. | Country code top level domain (e.g. england.co.uk) | |||
| ) | End of group # 1.1 | |||
| ) | End of group #1 |
Extracting the domain using our Pattern
If you execute our method using the following content :
www.subdomain.domain.com www.google.co www.google.co.in www.google.com www.facebook.com www.google.co.tw
Using the following sample code to execute our method :
domain :www.subdomain.domain.com
domain :www.google.co.in
domain :www.google.com
domain :www.facebook.com
domain :www.google.co.tw