loris has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

The following:

my $xpath = 'BoringNode[1]/InterestingNode[@InterestingAttribute="outg +rabe"]/AnotherBoringNode[@BoringAttribute="grey"]'; $xpath =~ /(InterestingNode\[.*?\])/; $1 =~ /"(.*)"/; print $1 . "\n";

does what I want, but I am sure that with backreferences I could do it in one step. However, my Googling hasn't helped me much. Can any one help me and/or point me to a really good RE tutorial on the web?

Update

Thinking about it (rather than some Googling for The Perfect RE Tutorial) makes me realise that I can do:

$xpath =~ /(InterestingNode\[@.*?"(.*?)".*?\])/; print $2 . "\n";

Any comments?

Thanks,

loris


"It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ."

Replies are listed 'Best First'.
Re: Extracting part of a substring with one regex (with backreference?)
by Samy_rio (Vicar) on Oct 28, 2005 at 08:52 UTC

    Hi loris, try this.

    my $xpath = 'BoringNode[1]/InterestingNode[@InterestingAttribute="outg +rabe"]/AnotherBoringNode[@BoringAttribute="grey"]'; print $xpath =~ /InterestingNode\[[^"]+\"([^"]+)\"\]/;

    Regards,
    Velusamy R.

      Thanks Samy_rio, but when I do

      print "$1\n";
      I get

      outg +rabe

      on my box.

      ?

      loris


      "It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ."
        Um, that's because you copied and paste the code as it appeared in your browser, and perlmonks.org defaults to wrapping code at 60 chars or so. Try the "d/l" (download code) link next time.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        I think that you have cut-and-pasted Samy_rio's code directly from the node, so you also copied the plus sign and embedded newline that the Perlmonks site added for code wrapping. Click the 'download' link, and you will see that his code is two lines, not three.