Simple RegEx question
Hi Guys,
I'm trying to use regex to see if a domain is a valid domain.
ex:
String domain = "somedomain.co.uk"
but could also be
String domain="somed@domain#(.co.uk"
The second one is invalid. And I only want to match strings between the start and the first period to ensure that they are a-z,A-Z,0-9 and a dash. If the domain contains invalid characters (not letter,number or dash) it should return nothing, otherwise it should return the domain.
I'm not worried about checking the TLD or ccTLD (extension).
Any examples?
Thanks!
Lucy
[608 byte] By [
Lucy83a] at [2007-10-3 3:35:06]

Well, I think what you want is something like
[a-zA-Z0-9][a-zA-Z0-9\\-]*\\..*
Let's break that down into pieces, shall we?
[a-zA-Z0-9]
[a-zA-Z0-9\\-]*
\\.
.*
The first item ensures that the domain name starts with an alphanumeric character.
The second item describes any number of alphanumeric or dash characters, including zero.
The third is a literal period.
The fourth indicates any number of any characters.
Does that make sense? Do you have any questions as to how any of those pieces work?
tvynra at 2007-7-14 21:29:45 >

Pattern p = Pattern.compile("^[a-z0-9]++(?:-[a-z0-9]++)*+(?=\\.)",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
if (m.find())
{
System.out.println(m.group());
}