Howto capture all matches of a single group

E

Ersin Er

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
 
M

Matija Papec

X-Ftn-To: Ersin Er

Ersin Er said:
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.
 
E

Ersin Er

Matija said:
X-Ftn-To: Ersin Er



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
 
A

A. Sinan Unur

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.

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

Sinan
 
M

Matija Papec

X-Ftn-To: Ersin Er

Ersin Er said:
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"
 
E

Ersin Er

A. Sinan Unur said:
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).
 
A

A. Sinan Unur

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
 
E

Ersin Er

A. Sinan Unur said:
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,
 
S

Sherm Pendley

Ersin Er said:
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--
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top