Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
We don't bite newbies here... much
 
PerlMonks  

Re: Skipping special tags in regexes

by delirium (Chaplain)
on Dec 04, 2003 at 13:25 UTC ( [id://312268]=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 Skipping special tags in regexes

Another possibility is to not rely on regexes. I'm not sure what your specs for searching and replacing are, but if they are narrowed down to prefixing words with other words, as in your example, you could get away with building a custom function and passing it the line to change, the word to look for, and what to prefix it with, e.g.:

#!/usr/bin/perl -w use strict; sub prefix { my ($line, $look_for, $prefix, $flip) = @_; return $line unless $look_for && $line =~ /$look_for/; my @arr = split /(<[^>]*>)/, $line; for my $cnt (0..$#arr) { if ($arr[$cnt] =~ /$look_for/) { $arr[$cnt-2] .= $prefix if $cnt > 1 && !$flip; $arr[$cnt+2] = $prefix . ' ' . $arr[$cnt+2] if $cnt < $#ar +r-1 && $flip; } } return join '', @arr; } my $line = "<5b>I <5c>like <5d>tacos\n"; print prefix ($line, 'tacos', 'yummy'); print prefix ($line, 'like', 'yummy', 1); __OUTPUT__ <5b>I <5c>like yummy<5d>tacos <5b>I <5c>like <5d>yummy tacos

Replies are listed 'Best First'.
Re: Re: Skipping special tags in regexes
by fletcher_the_dog (Friar) on Dec 04, 2003 at 16:04 UTC
    unfortunately, I have many regexes that are not fixed strings and require all the powers of regexes

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://312268]
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.