Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl Monk, Perl Meditation
 
PerlMonks  

Re: More Regexp Confusion

by johngg (Canon)
on Feb 01, 2007 at 18:54 UTC ( [id://597858]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to More Regexp Confusion

This seems to do what I think you want using your data.

use strict; use warnings; my @strings = ( q{asdfgGOATaewrjgn}, q{ererGOAskjgbrTslkgjnjng}, q{ccG<!-- comment on status of global geopolitical economics -->OAj +gTsvs}, q{aGO<h3>i hate clowns</h3>ATbbb}); my $needle = q{GOAT}; my @parts = split m{}, $needle; my $notNeedle = qq{[^$needle]*}; my $haystackPatt = q{(} . join($notNeedle, @parts) . q{)}; my $rxHaystack = qr{$haystackPatt}; my $stuff1 = q{^^^}; my $stuff2 = q{+++}; foreach my $string ( @strings ) { print qq{$string\n}; $string =~ s{$rxHaystack}{$stuff1$1$stuff2}; print qq{$string\n\n}; }

It produced this output.

asdfgGOATaewrjgn asdfg^^^GOAT+++aewrjgn ererGOAskjgbrTslkgjnjng erer^^^GOAskjgbrT+++slkgjnjng ccG<!-- comment on status of global geopolitical economics -->OAjgTsvs cc^^^G<!-- comment on status of global geopolitical economics -->OAjgT ++++svs aGO<h3>i hate clowns</h3>ATbbb a^^^GO<h3>i hate clowns</h3>AT+++bbb

I hope this is useful.

Cheers,

JohnGG

Update: Fixed typos

Update 2: Changed initialisation of $notNeedle so string was not hard coded, original line was my $notNeedle = q{[^GOAT]*};

Replies are listed 'Best First'.
Re^2: More Regexp Confusion
by mdunnbass (Monk) on Feb 05, 2007 at 09:59 UTC
    Thanks so much!

    I tried this code over the weekend, and it definitely does what I wanted it to. I made a few minor modifications, to suit my needs more specfically of course, but otherwise, you hit the nail on the head.

    Thanks again to everyone who helped!
    Matt

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://597858]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.