in reply to PCRE: Repeating Subpattens After Intervening Characters

merlyn's on-target comment aside, I'd normally do that by assigning the repeated pattern to a variable and interpolating that into the regex pattern.

# original: my $pat = "\d\s+foo"; # fixed: my $pat = "\\d\\s+foo"; my $regex = qr/$pat[^\r\n]*$pat/;

Not sure about PCRE, but whatever programming language you use, I suspect you can construct a string using variables similarly to build up to your pattern.

Update: Doh! Thanks, ikegami. I was trying to show the subpattern with strings for the cross-language analogy and slipped up.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: PCRE: Repeating Subpattens After Intervening Characters
by ikegami (Patriarch) on Sep 14, 2005 at 22:18 UTC
    That should be
    my $pat = "\\d\\s+foo";
    or better yet
    my $pat = qr/\d\s+foo/;

    Your pat incorrectly matches "dssssfoo", and doesn't match "1 foo" as it should.

    my $pat = "\d\s+foo"; print("$pat\n"); # ds+foo
Re^2: PCRE: Repeating Subpattens After Intervening Characters
by schnarff (Acolyte) on Sep 14, 2005 at 22:09 UTC
    Wow, you guys are amazingly quick to reply and helpful in those replies. That said, since I need to do this outside of Perl, in a pure PCRE expression, neither this nor ikegami's post is helpful.

    Seeing as how I just looked and found no PCREmonks, however, does anyone know if such a forum actually exists and I'm just bad at Googling, or if a forum with such a purpose exists under some different name/URL?

    Thanks Again,
    Alex
      Wow, you guys are amazingly quick to reply and helpful in those replies. That said, since I need to do this outside of Perl, in a pure PCRE expression, neither this nor ikegami's post is helpful.

      I disagree. We didn't use anything Perl-specific. For example, here it is in JScript:

      var pat = "\\d\\s+foo"; var regexp = "(" + pat + ")[^\r\n]*(" + pat + ")"; var re = new RegExp(regexp); re.exec(some_string);

      (Untested)