Asmor
Asmor

Reputation: 5181

Multiple captures with one set of parens in a larger regex

I've got a regular expression (javascript) which is something like...

/(x)(y)+(z)/gi

The problem is that I'll always get exactly 3 captures from those parens, even if the (y)+ matched multiple times. If it does match multiple times, it just returns the last match. I've no way of knowing ahead of time how many times y will match on any given run, but I want to capture all of them.

Any ideas?

Upvotes: 1

Views: 758

Answers (3)

harpo
harpo

Reputation: 43228

I take it you can't use

/(x)((?:y)+)(z)/gi

because this is part of a "larger regex"?

Upvotes: 3

Chas. Owens
Chas. Owens

Reputation: 64949

Move the + inside of the parentheses and then split y into its individual parts. The following is Perl, but it should give you an idea:

#!/usr/bin/perl

use strict;
use warnings;

my $s = "abcbcbcd";

my ($x, $y, $z) = $s =~ /(a)((?:bc)+)(d)/;

my @y = $y =~ /(bc)/g;

print "x is $x\n",
    "y is ", join(", ", @y), "\n",
    "z is $z\n";

And here is some crappy Javascript I hacked together (I don't really know Javascript):

<html>
<body>

<script type="text/javascript">
var str = "abcbcbcd"; 
var matches = str.match(/(a)((?:bc)+)(d)/);
var x = matches[1];
var y = matches[2].match(/(bc)/g);
var z = matches[3];

document.write(
    "x is ", x, "<br />",
    "y is ", y.join(", "), "<br />",
    "z is ", z
);
</script>

</body>
</html>

Upvotes: 2

David Z
David Z

Reputation: 131800

I would use

/(x)(y+)(z)/gi

then take the text that matched the second group and parse it further.

Upvotes: 4

Related Questions