[regex] how to pre-determine group count?

Discussion in 'Java' started by Timo Nentwig, Dec 23, 2003.

  1. Timo Nentwig

    Timo Nentwig Guest

    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
     
    Timo Nentwig, Dec 23, 2003
    #1
    1. Advertising

  2. Timo Nentwig

    Brian Palmer Guest

    Timo Nentwig <> writes:

    > 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);
    }


    --
    See comp.lang.java.announce for java-related announcements
     
    Brian Palmer, Dec 23, 2003
    #2
    1. Advertising

  3. "Timo Nentwig" <> wrote in message
    news:bs928c$a4c4q$-berlin.de...
    > 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.. ]

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Dec 23, 2003
    #3
  4. Timo Nentwig

    nos Guest

    "Timo Nentwig" <> wrote in message
    news:bs928c$a4c4q$-berlin.de...
    > 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
     
    nos, Dec 23, 2003
    #4
  5. Timo Nentwig

    Alan Moore Guest

    On 23 Dec 2003 02:57:50 -0800, Brian Palmer
    <> wrote:

    >Timo Nentwig <> writes:
    >
    >> 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("\\.");
     
    Alan Moore, Dec 24, 2003
    #5
  6. Timo Nentwig

    Timo Nentwig Guest

    Alan Moore wrote:

    > 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("\\.");


    You are right, thanks!
     
    Timo Nentwig, Dec 24, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rocky Moore
    Replies:
    7
    Views:
    1,750
    mikeb
    Jan 14, 2004
  2. =?Utf-8?B?S2V2aW4gQnVydG9u?=

    Pre-Send Request Headers, Pre-Send Request Content

    =?Utf-8?B?S2V2aW4gQnVydG9u?=, Dec 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,063
    =?Utf-8?B?S2V2aW4gQnVydG9u?=
    Dec 31, 2004
  3. christopher taylor
    Replies:
    0
    Views:
    411
    christopher taylor
    Sep 17, 2008
  4. RolfK
    Replies:
    1
    Views:
    1,867
    Martin Honnen
    Jun 7, 2009
  5. Chris M. Thomasson
    Replies:
    10
    Views:
    656
    Nobody
    Aug 29, 2009
Loading...

Share This Page