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

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.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Substitution Problem
by Django (Pilgrim) on Sep 13, 2002 at 07:25 UTC

    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.