Re: search and replace using regex

From: Drieux (drieux_at_wetware.com)
Date: 12/03/03


Date: Wed, 3 Dec 2003 10:20:52 -0800
To: Perl Perl <beginners@perl.org>


On Dec 3, 2003, at 7:06 AM, Rob Dixon wrote:
[..]
>> remember that those double quotes will allow things
>> to be 'interpolated' - which is not what you want.
>
> No they won't: they're not string delimiters in this context
> so they'll be treated as any other non-special character between
> the real s/// slash delimiters.
>
> Rob

Minor Blonde Hair Moment On my phrasing.
Thanks You for the good catch!

let's look at some code:

        my $foo = q|" _</pat stuff _</pat> other stuff|;
        
        print "$foo \n";
        
        $foo =~ s|" _</pat|"</pat"|g;
        print "$foo \n";

which will generate

" _</pat stuff _</pat> other stuff
"</pat" stuff _</pat> other stuff

Now IF the OP really wanted that sort of case,
then while a bit wacko, it will work.

So the question is whether that '"' double
quote token is a part of the pattern that
will be searched for, or is it the sort of
classic cut and paste problem of say

        my $foo = " _</pat .... _</pat";

and in the heat of trying to sort out what was
scragged in the RegEx, hoping over the line to

        $_ =~ s/"_\</pat"/"\</pat"/gi; #g for every occurrence, i for

hoping that by adding in more tokens it
would make it go zoom zoom..

I think that were you or I working the RE
and we wanted to remove ' _<' and replace it
with mere '<' we would have done say

        $_ =~ s| _<|<|g;

Or we might use say

        $_ =~ s|\s*_<|<|g;

to clean out the preceeding 'white space'...

ciao
drieux

---


Relevant Pages

  • Re: Same sex marriage?
    ... >>> king desireth not any dowry, but an hundred foreskins of the ... >> the relationship between king Saul of Israel and David. ... > What do you mean that I quoted out of context? ... My quotes are literal. ...
    (soc.culture.netherlands)
  • Re: Kentuckys Well-Regulated Militia Bring Food & Water To Disaster Victims
    ... is a dishonest coward who had previously asked for such quotes and he ... people to keep up a militia and not have it disarmed by the feds. ... Also provide full context because these writers ... It's the truth because I've read it in the original documents and ...
    (talk.politics.guns)
  • Re: Kentuckys Well-Regulated Militia Bring Food & Water To Disaster Victims
    ... who had previously asked for such quotes and he will now ignore them. ... people to keep up a militia and not have it disarmed by the feds. ... Also provide full context because these writers ... It's the truth because I've read it in the original documents and ...
    (talk.politics.guns)
  • Re: OT but damn they love to bomb
    ... The quotes have a context of their own that is not ... But, we're not winning, and an exit strategy is ... >>strategy before victory tends to assure losing. ...
    (rec.arts.dance)
  • Re: My Very Strange Webhost, SBI! -- Opinions, Please
    ... Correctly attribute all your quotes. ... you often trim too much and so fail to provide context for your responses. ... in a discussion where the post before the one that is being replied to was quoted to provide context for its response your quoting only the response deprives it of its contexts and so removes the context of your response. ... But the point of using quotes of preceding posts to provide context for their responses is to avoid the need to re-establish the context in the response. ...
    (comp.lang.javascript)