On Tue, Apr 14, 2015 at 08:58:29PM -0500, Patrick R. Michaud wrote: > Just an idea: instead of building strings to be interpolated into > a regex, could you just build regexes directly? > > my $pattern = rx/$<greeting>=[hello]/; > my $match = "hello" ~~ / <pattern=$pattern> /; > > The resulting string is captured into $match<pattern><greeting>; > The second statement can also be written as: > > # captures into $match<pattern><greeting> > my $match = "hello" ~~ / $<pattern>=$pattern /; > > # captures into $match[0]<greeting> > my $match = "hello" ~~ / $<0>=$pattern /; > > Hope this is useful, or at least illustrative.
I had given up on using regexes embedded within regexes, because I could not get capturing to work. I put together some test cases, to try to show what I would expect to work. A bunch of the ones that I was planning to use do not work. I do not know if that is because they have not been implemented, or whether they are designed to not work this way. In either case, I am looking for a solution that allows me to define several regexes, then compose them into a single regex that captures by name. The solution must be flexible enough to allow me to combine the regexes in whatever order I need at the time, and always capture by name. -kolibrie use v6; use Test; my $year = '2015'; my $month = '04'; my $day = '11'; my $date_string = "$year-$month-$day"; my $year_regex = rx/$<year>=[\d**4]/; my $month_regex = rx/$<month>=[\d\d?]/; my $day_regex = rx/$<day>=[\d\d?]/; my $separator = rx/'-'/; # Single named capture. { ok($date_string ~~ $year_regex, 'date string matches year regex'); is(~$/, $year, 'matched is year string'); is(~$/<year>, $year, 'year is captured'); } # Single named capture in slashes. { ok($date_string ~~ /$year_regex/, 'date string matches year regex when in slashes'); is(~$/, $year, 'matched is year string when in slashes'); is(~$/<year>, $year, 'year is captured when in slashes'); # Fails } # Single named capture embedded in named capture. { ok($date_string ~~ /$<pattern>=$year_regex/, 'date string matches year regex when embedded'); is(~$/, $year, 'matched is year string when embedded'); is(~$/<pattern>, $year, 'pattern is captured when embedded'); is(~$/<pattern><year>, $year, 'year is captured when embedded'); } # Single named capture embedded in named capture with brackets. { ok($date_string ~~ /$<pattern>=[$year_regex]/, 'date string matches year regex when embedded with brackets'); is(~$/, $year, 'matched is year string when embedded with brackets'); is(~$/<pattern>, $year, 'pattern is captured when embedded with brackets'); is(~$/<pattern><year>, $year, 'year is captured when embedded with brackets'); # Fails } # Multiple named captures. { ok($date_string ~~ /$year_regex $separator $month_regex $separator $day_regex/, 'date string matches multiple regexes in slashes'); is(~$/, $date_string, 'matched is date string in multi regex with slashes'); is(~$/<year>, $year, 'year is captured in multi regex with slashes'); # Fails is(~$/<month>, $month, 'month is captured in multi regex with slashes'); # Fails is(~$/<day>, $day, 'day is captured in multi regex with slashes'); # Fails } # Multiple named captures embedded in named capture. { ok($date_string ~~ /$<pattern>=$year_regex $separator $month_regex $separator $day_regex/, 'date string matches multiple regexes when embedded'); is(~$/, $date_string, 'matched is date string in multi regex when embedded'); is(~$/<pattern>, $year, 'pattern is captured in multi regex when embedded'); is(~$/<pattern><year>, $year, 'year is captured in multi regex when embedded'); is(~$/<month>, $month, 'month is captured in multi regex when embedded'); # Fails is(~$/<day>, $day, 'day is captured in multi regex when embedded'); # Fails } # Multiple named captures embedded in named capture with brackets. { ok($date_string ~~ /$<pattern>=[$year_regex $separator $month_regex $separator $day_regex]/, 'date string matches multiple regexes when embedded with brackets'); is(~$/, $date_string, 'matched is date string in multi regex when embedded with brackets'); is(~$/<pattern>, $date_string, 'pattern is captured in multi regex when embedded with brackets'); is(~$/<pattern><year>, $year, 'year is captured in multi regex when embedded with brackets'); # Fails is(~$/<pattern><month>, $month, 'month is captured in multi regex when embedded with brackets'); # Fails is(~$/<pattern><day>, $day, 'day is captured in multi regex when embedded with brackets'); # Fails }
signature.asc
Description: Digital signature