Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Construct a REGEX by selected substitution

by pysome (Scribe)
on Sep 29, 2007 at 05:16 UTC ( #641667=perlquestion: print w/replies, xml ) Need Help??

pysome has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I wanna to replace these "TXT" ONLY locate out of TAGS to "FOO", How to compose the regex??
my $str = '<a code=TXT> abc TXT </a> <text code=TXT> efg TXT </text> < +input s=TXT> hig TXT</input>';
After substitution,I want to get $str is :
<a code=TXT> abc FOO </a> <text code=TXT> efg FOO </text> <input s=TXT +> hig FOO</input>
Thanks in advance.
update:NO while loop

Replies are listed 'Best First'.
Re: Construct a REGEX by selected substitution
by planetscape (Chancellor) on Sep 29, 2007 at 09:01 UTC
Re: Construct a REGEX by selected substitution
by jeanluca (Deacon) on Sep 29, 2007 at 08:11 UTC
    Have a look at perlre which helps me all the time!
    Especially the part Extended Patterns (which I've used here) is very interesting!

    For the string you submitted the following will do
    $str =~ s/(?<=\s)TXT/FOO/g ;
    CHeers
    LuCa

    UPDATE: this one might be easier
    $str =~ s/ TXT/ FOO/g ;
      How to deal if the $str is :
      '<a code=" TXT"> abc =" TXT </a> <text code= TXT> efg =" TXT </text> < +input s=" TXT"> hig =" TXT</input>';
      Thanks,

        It looks like what you really need to do is parse some HTML and manipulate some of the elements of the parsed text. However it's not clear just what you want to be able to manipulate, particularly as the sample you've given isn't a well formed fragment of HTML - you seem to have mismatched quote characters. It doesn't help that it's not clear just which TXT instances in the sample ought to be replaced.

        For modest sized documents HTML::TreeParser may be the most appropriate tool for the job.


        Perl is environmentally friendly - it saves trees
        Did you read the perlre documentation ? It contains the answer to your question!
        Show us what you've tried or tell us whats unclear about the documentation!

        LuCa
Re: Construct a REGEX by selected substitution
by sanPerl (Friar) on Sep 29, 2007 at 17:21 UTC
    This will help you. I always use this technique.
    my $str = '<a code=TXT> abc TXT </a> <text code=TXT> efg TXT </text> < +input s=TXT> hig TXT</input>'; print "Before: ",$str,"\n#########\n"; $str =~ s{(>)(.*?)(<)} { my ($tag_ends, $mydata, $tag_starts) = ($1,$2,$3); $mydata =~ s/TXT/FOO/gs; "$tag_ends$mydata$tag_starts" }exgs; print "After: ",$str,"\n#########\n";
    Let me know if you need any more help.
      Thank you very much. Although it is verbose by modifier :/ex.
Re: Construct a REGEX by selected substitution
by Anonymous Monk on Sep 29, 2007 at 06:28 UTC
    How to compose the regex??
    Have you ever composed a regex?
Re: Construct a REGEX by selected substitution
by pysome (Scribe) on Sep 29, 2007 at 10:12 UTC
    Is it really a challenge?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2023-03-26 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (63 votes). Check out past polls.

    Notices?