Howto capture all matches of a single group

Discussion in 'Perl Misc' started by Ersin Er, Oct 2, 2005.

  1. Ersin Er

    Ersin Er Guest

    Hi,

    I'm a Java programmer but I think my questions applies Perl also while
    Regular Expressions in Java are almost Perl compatible.

    Say, I have a regexp like (\d)(?:,(\d))* which recognizes inputs like
    "1", "0,4", "9,2,0", etc. What I want is to get all captured strings
    from group 3 (and also the one from group 1). When the operation has
    finished group 3 has only the last captured text which is the last
    digit for this case. However I want to get all digits matched by group
    1 and 3.

    You may advise to use splitting but it does not work for more complex
    examples where terminators are also included in the subgroups several
    times.

    Any suggestions?

    Thanks in advance.

    -- Ersin
     
    Ersin Er, Oct 2, 2005
    #1
    1. Advertising

  2. Ersin Er

    Matija Papec Guest

    X-Ftn-To: Ersin Er

    "Ersin Er" <> wrote:
    >I'm a Java programmer but I think my questions applies Perl also while
    >Regular Expressions in Java are almost Perl compatible.
    >
    >Say, I have a regexp like (\d)(?:,(\d))* which recognizes inputs like
    >"1", "0,4", "9,2,0", etc. What I want is to get all captured strings
    >from group 3 (and also the one from group 1). When the operation has
    >finished group 3 has only the last captured text which is the last
    >digit for this case. However I want to get all digits matched by group
    >1 and 3.


    perl -e "print '9,2,0,1' =~ /(\d)(?:,(\d)*)/g"

    >You may advise to use splitting but it does not work for more complex
    >examples where terminators are also included in the subgroups several
    >times.


    "perldoc -f split" can split on regexes (and capture them if you want), so
    most probably you could use it as well.



    --
    Matija
     
    Matija Papec, Oct 2, 2005
    #2
    1. Advertising

  3. Ersin Er

    Ersin Er Guest

    Matija Papec wrote:
    > X-Ftn-To: Ersin Er
    >
    > "Ersin Er" <> wrote:
    > >I'm a Java programmer but I think my questions applies Perl also while
    > >Regular Expressions in Java are almost Perl compatible.
    > >
    > >Say, I have a regexp like (\d)(?:,(\d))* which recognizes inputs like
    > >"1", "0,4", "9,2,0", etc. What I want is to get all captured strings
    > >from group 3 (and also the one from group 1). When the operation has
    > >finished group 3 has only the last captured text which is the last
    > >digit for this case. However I want to get all digits matched by group
    > >1 and 3.

    >
    > perl -e "print '9,2,0,1' =~ /(\d)(?:,(\d)*)/g"


    Yes, I've tried this and that's good that perl can capture all matched
    strings by a group. However this is not the case for Java using the
    group() method :(

    Thanks.

    -- Ersin
     
    Ersin Er, Oct 2, 2005
    #3
  4. "Ersin Er" <> wrote in
    news::

    >
    > Matija Papec wrote:
    >> X-Ftn-To: Ersin Er
    >>
    >> "Ersin Er" <> wrote:
    >> >I'm a Java programmer but I think my questions applies Perl also
    >> >while Regular Expressions in Java are almost Perl compatible.
    >> >
    >> >Say, I have a regexp like (\d)(?:,(\d))* which recognizes inputs
    >> >like "1", "0,4", "9,2,0", etc. What I want is to get all captured
    >> >strings from group 3 (and also the one from group 1). When the
    >> >operation has finished group 3 has only the last captured text which
    >> >is the last digit for this case. However I want to get all digits
    >> >matched by group 1 and 3.

    >>
    >> perl -e "print '9,2,0,1' =~ /(\d)(?:,(\d)*)/g"

    >
    > Yes, I've tried this and that's good that perl can capture all matched
    > strings by a group. However this is not the case for Java using the
    > group() method :(


    Which, if true, would be a good case in point against posting Java
    questions in a Perl group. OTOH,

    http://java.sun.com/docs/books/tutorial/extra/regex/groups.html

    seems to indicate that it is indeed possible to capture all the matches.

    > -- Ersin


    Your sig separator is incorrect. It should be dash-dash-space-newline.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 2, 2005
    #4
  5. Ersin Er

    Matija Papec Guest

    X-Ftn-To: Ersin Er

    "Ersin Er" <> wrote:
    >> >from group 3 (and also the one from group 1). When the operation has
    >> >finished group 3 has only the last captured text which is the last
    >> >digit for this case. However I want to get all digits matched by group
    >> >1 and 3.

    >>
    >> perl -e "print '9,2,0,1' =~ /(\d)(?:,(\d)*)/g"

    >
    >Yes, I've tried this and that's good that perl can capture all matched
    >strings by a group. However this is not the case for Java using the
    >group() method :(


    /g switch make regex match all occurrences within a string, so if I must
    guess for java, that would be findAll or matchAll method?

    btw, the above regex could actually be much simpler,
    perl -e "print '9,2,0,1' =~ /(\d+)/g"


    --
    Matija
     
    Matija Papec, Oct 2, 2005
    #5
  6. Ersin Er

    Ersin Er Guest

    A. Sinan Unur wrote:

    > Which, if true, would be a good case in point against posting Java
    > questions in a Perl group. OTOH,
    >
    > http://java.sun.com/docs/books/tutorial/extra/regex/groups.html
    >
    > seems to indicate that it is indeed possible to capture all the matches.


    If you look at

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#cg

    it says "The captured input associated with a group is always the
    subsequence that the group most recently matched."

    Sorry if this thread is really unrelated to Perl (or Regexp in
    particular).

    --
    Ersin
     
    Ersin Er, Oct 2, 2005
    #6
  7. "Ersin Er" <> wrote in
    news::

    >
    > A. Sinan Unur wrote:
    >
    >> Which, if true, would be a good case in point against posting Java
    >> questions in a Perl group. OTOH,
    >>
    >> http://java.sun.com/docs/books/tutorial/extra/regex/groups.html
    >>
    >> seems to indicate that it is indeed possible to capture all the
    >> matches.

    >
    > If you look at
    >
    > http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#c

    g
    >
    > it says "The captured input associated with a group is always the
    > subsequence that the group most recently matched."


    Well, here is the deal. Apparently, Java regexes are weird. (Please bear
    with me for minute):

    D:\Home\asu1\UseNet\clpmisc> cat MyTest.java
    import java.util.regex.*;

    public class MyTest {
    public static void main(String[] args) {
    Pattern p = Pattern.compile("(9)");
    Matcher m = p.matcher("9,2,0,1");
    if (m.matches()) {
    for (int i = 1; i != m.groupCount(); ++i) {
    System.out.println(m.group(i));
    }
    } else {
    System.out.println("No match");
    }
    }
    }

    D:\Home\asu1\UseNet\clpmisc> javac MyTest.java

    D:\Home\asu1\UseNet\clpmisc> java MyTest
    No match

    D:\Home\asu1\UseNet\clpmisc> javac -version
    javac 1.5.0_04

    I fail to comprehend this.

    OK, I am shutting up now. You might want to go over to
    comp.java.programmer.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 2, 2005
    #7
  8. Ersin Er

    Ersin Er Guest

    A. Sinan Unur wrote:
    >
    > Well, here is the deal. Apparently, Java regexes are weird. (Please bear
    > with me for minute):
    >
    > D:\Home\asu1\UseNet\clpmisc> cat MyTest.java
    > import java.util.regex.*;
    >
    > public class MyTest {
    > public static void main(String[] args) {
    > Pattern p = Pattern.compile("(9)");
    > Matcher m = p.matcher("9,2,0,1");
    > if (m.matches()) {
    > for (int i = 1; i != m.groupCount(); ++i) {
    > System.out.println(m.group(i));
    > }
    > } else {
    > System.out.println("No match");
    > }
    > }
    > }
    >
    > D:\Home\asu1\UseNet\clpmisc> javac MyTest.java
    >
    > D:\Home\asu1\UseNet\clpmisc> java MyTest
    > No match
    >
    > D:\Home\asu1\UseNet\clpmisc> javac -version
    > javac 1.5.0_04
    >
    > I fail to comprehend this.


    Well, you may try:

    import java.util.regex.*;

    public class MyTest {
    public static void main(String[] args) {
    Pattern p = Pattern.compile("(9)");
    Matcher m = p.matcher("9,2,0,1");
    if (m.lookingAt()) {
    for (int i = 1; i <= m.groupCount(); ++i) {
    System.out.println(m.group(i));
    }
    } else {
    System.out.println("No match");
    }
    }
    }

    It just works. The behaviour of methods (matches(), lookingAt()) are
    explained in Java API documentation.

    Anyways, I see here is not the correct list to ask this question. Sorry
    for uninteresting traffic.

    Cheers,

    --
    Ersin
     
    Ersin Er, Oct 2, 2005
    #8
  9. "Ersin Er" <> writes:

    > Yes, I've tried this and that's good that perl can capture all matched
    > strings by a group. However this is not the case for Java


    If you want a Java answer, ask the question in a Java group.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Oct 2, 2005
    #9
    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. Replies:
    3
    Views:
    1,564
  2. James Collier
    Replies:
    4
    Views:
    1,093
    Erik Heneryd
    Aug 12, 2004
  3. Fredrik Lundh
    Replies:
    0
    Views:
    392
    Fredrik Lundh
    Aug 11, 2004
  4. christopher taylor
    Replies:
    0
    Views:
    408
    christopher taylor
    Sep 17, 2008
  5. weston
    Replies:
    1
    Views:
    94
    Michael Winter
    Jun 28, 2005
Loading...

Share This Page