in reply to Matching Character Patterns With Backreferences

I'm not a regexp wizard, but playing around with this I've found that if you use $1 and friends, they are populated until a submatch fails (this does not happen with \1 form). No idea why this doesn't fail the whole expression, but at least it suggests a workaround until someone explains what's really going on:

my @matches = $word =~ /^(\w)([^$1])($2)([^$1$2$3])([^$1$2$3$4])([^$1$ +2$3$4$5])/; print $word if @matches == 6; # or length $word, whatever.

BTW, what's that \s doing in your regexp?

Replies are listed 'Best First'.
Re^2: Matching Character Patterns With Backreferences
by Anonymous Monk on Dec 05, 2004 at 21:12 UTC

    I'm having trouble getting this to work with the $1 form - it's simply not matching anything the moment I make that change.

    The \s is simply because each line of the dictionary file contains a word followed by whitespace. Checking for the space stops me from printing out "TESTING" when I only want "TEST" :-)

      You should be using \1 and friends in the left hand side of the s/// operator.
        Are you sure? I believe it's the other way around; you should use $1 and friends on the right hand side of the s/// operator. That's what the warning in perlre is about, anyway.