[regex] how to pre-determine group count?

T

Timo Nentwig

Hi!

I need to convert an domain name into an array:

www.inter.net -> { "www", "inter", "net" }

I want to use a regex but don't get it how to pre-determine the number of
groups (i.e. dots+1). Sample program:

public static void main(String[] args)
{
String ip = "a.bc.def.g.h";

Pattern pat = Pattern.compile("(\\w)+");
Matcher mat = pat.matcher(ip);
System.out.println(mat.groupCount()); // == 1
while (mat.find())
{
System.out.println(mat.group());
}
}

Isn't it possible to pre-determine the group count?

Regards
Timo
 
B

Brian Palmer

Timo Nentwig said:
Hi!

I need to convert an domain name into an array:

www.inter.net -> { "www", "inter", "net" }

I want to use a regex but don't get it how to pre-determine the number of
groups (i.e. dots+1). Sample program:

I suggest you look at Pattern.split() for that. E.g.,
String ip = "a.bc.def.g.h";

Pattern pat = Pattern.compile("\\W+");
String[] ns = pat.split(ip);
for (int i = 0; i < ns.length; i++) {
System.out.println(ns);
}
 
A

Andrew Thompson

Timo Nentwig said:
Hi!

I need to convert an domain name into an array:

www.inter.net -> { "www", "inter", "net" }

I want to use a regex

...could you clarify if this is an exercise in
understanding regex's Timo? That seems the
case (but I am not entirely certain).

[ I have no experience w/regex's but
wonderred why you do not just use a
StringTokenizer.. ]
 
N

nos

Timo Nentwig said:
Hi!

I need to convert an domain name into an array:

www.inter.net -> { "www", "inter", "net" }

I want to use a regex but don't get it how to pre-determine the number of
groups (i.e. dots+1). Sample program:

public static void main(String[] args)
{
String ip = "a.bc.def.g.h";

Pattern pat = Pattern.compile("(\\w)+");
Matcher mat = pat.matcher(ip);
System.out.println(mat.groupCount()); // == 1
while (mat.find())
{
System.out.println(mat.group());
}
}

Isn't it possible to pre-determine the group count?

Regards
Timo

how come you don't match on "dot" then
 
A

Alan Moore

Timo Nentwig said:
Hi!

I need to convert an domain name into an array:

www.inter.net -> { "www", "inter", "net" }

I want to use a regex but don't get it how to pre-determine the number of
groups (i.e. dots+1). Sample program:

I suggest you look at Pattern.split() for that. E.g.,
String ip = "a.bc.def.g.h";

Pattern pat = Pattern.compile("\\W+");
String[] ns = pat.split(ip);
for (int i = 0; i < ns.length; i++) {
System.out.println(ns);
}


But using "\\W+" would break on a domain name with embedded dashes.
You're better off with this:

String ip = "www.regular-expressions.info";
String[] ipa = ip.split("\\.");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top