What if you have <> inside your tags? Text::Delimited can solve a lot of your problems for this kind of parsing. More info here.

#! usr/bin/perl use strict; use warnings; use Text::Balanced qw/extract_bracketed/; while( <DATA> ) { my $in = $_; print "\nIN : $in"; ## input my $out = $in; ## OK if you never have \<\> inside \<\> $out =~ s/\<([^\>]+)\>/\n\{$1\}\n/g; print "\tOUT 1 : $out"; ## simple output ## Have to be more clever if it does ## extract balanced delimiters my @extracted = extract_bracketed($in, '<>'); if (!defined$extracted[0]){ ## undefined if no balanced delimiters existed ## so print straight out print "\tOUT 2 : $in"; } else { ## balanced delimters were found, ## so process them as before print "\tOUT 2 : "; for (@extracted){ s/^\<(.+)\>$/\n\{$1\}\n/g; print $_; } } } __DATA__ [SOUR] Por Gisela Orozco 312.527.8461/ Chicago\ <line> Por Gisela Orozco< ttl>312.527.8461/ Chicago</ttl> <<test>>extra bumphf

Will produce the right output more robustly (see the last input line) :

IN : [SOUR] OUT 1 : [SOUR] OUT 2 : [SOUR] IN : Por Gisela Orozco 312.527.8461/ Chicago\ OUT 1 : Por Gisela Orozco 312.527.8461/ Chicago\ OUT 2 : Por Gisela Orozco 312.527.8461/ Chicago\ IN : <line> Por Gisela Orozco< ttl>312.527.8461/ Chicago</ttl> OUT 1 : {line} Por Gisela Orozco { ttl} 312.527.8461/ Chicago {/ttl} OUT 2 : {line} Por Gisela Orozco< ttl>312.527.8461/ Chicago</ttl> IN : <<test>>extra bumphf OUT 1 : {<test} >extra bumphf OUT 2 : {<test>} extra bumphf

HTH

Just a something something...

In reply to Re: replace string by BioLion
in thread replace string by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.