Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: Re: Substitution Problem

by Django (Pilgrim)
on Sep 10, 2002 at 08:05 UTC ( [id://196578]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Substitution Problem
in thread Substitution Problem

Don't confuse matching with capturing:

s/$a/$b/ # $a matches and is replaced by $b s/($a)/$1$b/ # $a matches and is captured in $1 # $b is appended
Note also, that you can't use character classes like "." or "\s" within the right side of the substitute operator.

~Django
"Why don't we ever challenge the spherical earth theory?"

Replies are listed 'Best First'.
Re: Re: Re: Re: Substitution Problem
by Wassercrats (Initiate) on Sep 13, 2002 at 02:42 UTC
    It crossed my mind that maybe I can't use character classes on the right side. I guess that was my only problem, but I think that should be allowed. If want delete all attributes from an anchor tag that's contained in @links1, except for href, it would be nice if the following (from my original message) would work:
    @links1=~s/.*/href\s*\=\s*\".*?\"/is;

    with a slight adjustment if I want the angle brackets.

      That wouldn't make much sense, because a character class can match any of its characters, but how should it be interpreted in the right side of s///? Should that mean all the characters it represents? Or a random one?
      The following code does what you want:

      for (@links1) { s/<\s*a\s.*?\b(href\s*=\s*".*?").*?>/<a $1>/i; }
      Now let me explain your errors:
      @links1=~ # that would work like scalar(@array)=~ s/ .* # replace complete string with / href # 'href' \s* # as many spacey chars as possible \= # no need to escape '=' \s* # as many spacey chars as possible \" # no need to escape '"' .*? # as few as possible (=no) anythings \" # no need to escape '"' /isx; # 's' is not needed, I added 'x'

      ~Django
      "Why don't we ever challenge the spherical earth theory?"

        I'm a beginner in Perl, but I think your breakdown of my code explains exactly what I want it to do. I went a little crazy with the escapes though. I'll need the s modifer for some other cases, though it's unnecessary for this example. I'd like the special meanings of the escaped characters on the right of the s/// to be used. I want the element in which the substitution is taking place (@links1[whatever]) to be referred to when Perl is assiging real text to the special characters. Not that you're solution is worse than my proposed one, but I was suprised to learn that mine didn't work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://196578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-16 20:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found