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

#!/usr/bin/perl use strict; while( <DATA> ) { s/<(\w+)>/\{$1\}\n/g; print $_; } [SOUR] Por Gisela Orozco 312.527.8461/ Chicago\ <line> Por Gisela Orozco< ttl>312.527.8461/ Chicago</ttl>
When ever the $_ has <characters> between '<' and '>' I have to replace it with {characters} and place the characters below the line. The output should look as below. for example:
[SOUR] Por Gisela Orozco 312.527.8461/ Chicago\ {line} Por Gisela Orozco {ttl} 312.527.8461/ Chicago

Replies are listed 'Best First'.
Re: replace string
by jethro (Monsignor) on Aug 19, 2009 at 13:14 UTC

    You forgot to mention what's not working

    In your case it is a missing line

    __DATA__

    right before the example data, and a regex you might change to

    s/<([^>]*)>/\{$1\}\n/g;

    or alternatively

    s/<(.*?)>/\{$1\}\n/g;

    Note that this is probably still short of what you want, since you seem to need to throw away ending tags. In that case you could add '/' to the character class above and throw away the ending tags in a second regex

      I think this is a better regex to solve this problem:

      s!\s*<\s*(/?)\s*(\w+)\s*>\s*!$1?"\n":"\n\{$2\}\n"!ge;

      because </ttl> should not appear (from the example's output) and there is a problem with extra white spaces surrounding and inside tags. Also, there was no info on what to do if there is more text after a closing tag, so I guessed that a new line must be inserted.

      But that regex will generate extra empty lines if a data line starts with a tag. Changing to this should do the trick:

      s!\s*<\s*(/?)\s*(\w+)\s*>\s*!$1?"\n":"\n\{$2\}\n"!ge and s!^\n!!;

      Please note that I kept the \w+ for the capture, asuming that only alphanumerics are admited as tags.

Re: replace string
by BioLion (Curate) on Aug 19, 2009 at 13:55 UTC

    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) :

    HTH

    Just a something something...
Re: replace string
by Marshall (Canon) on Aug 19, 2009 at 14:14 UTC
    Quite frankly, you appear to have some malformed HTML code that you want to clean up and make more human readable.

    Moving away from say <ttl>blah..blah</ttl> to a less standard format of {ttl}blah..blah, is in my opinion a mistake.

    There are lots of modules that can deal with: <ttl>blah..blah</ttl>. So I opted below to do what I could to "clean things up". I don't think that what you are asking for is a good idea. Of course there could be things that I don't know.

    #!/usr/bin/perl -w use strict; while( <DATA> ) { s/<\s*/</g; #compress spaces right after "< xxx" s/\s*>/>/g; #compress spaces right before "xxx >" s/(.)</$1\n</g; #add \n before < if not already on new line print $_; } # prints: # [SOUR] # Por Gisela Orozco 312.527.8461/ Chicago\ # <line> Por Gisela Orozco # <ttl>312.527.8461/ Chicago # </ttl> __DATA__ [SOUR] Por Gisela Orozco 312.527.8461/ Chicago\ <line> Por Gisela Orozco< ttl>312.527.8461/ Chicago</ttl >
      #!/usr/bin/perl use strict; use warnings; my $tag; my $output; my $fh; my $deletestrings; while (<DATA>) { s/\s*<\s*(\/?)\s*(\w+)\s*>\s*/$1?"\n":"\n\{$2\}\n"/ge; chomp; s/[\cA-\cZ]//g; # To remove control characters s/^[\\|<]$//g; # To delete the character like \ and < at the e +nd of the line s/[\\|<]$//; # To delete the character like \ and < at the beg +ining of the line s/^\s+//g; # To remove multiple spaces at the begining of the + line s/\s+$//g; # To remove spaces at the end of the line if(/^{(.*)}$/) { # match {METATAG} line $fh = output($output, $tag, $fh); $output = ""; $tag = $1; } else { # not a {TAG} line next unless($tag); next if(/^\s*$/); s/\\//g; $output .= ($output) ? " $_" : "<$tag>$_"; } } $fh = output($output, $tag, $fh); if($fh) { print $fh "</ROOT>\n"; close($fh); } exit(0); # Subroutine to open the file with the filename as {SOURCETAG} # Subroutine to open the file with the filename as {SOURCETAG} sub output { my ($output, $tag, $fh) = @_; if($output) { if($output =~ m/<SOURCE>(.*)/) { if($fh) { print $fh "</ROOT>\n"; close($fh); } open($fh, '>', "$1.xml") or die "$1.xml: $!"; print $fh "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<RO +OT>\n"; } print $fh "$output</$tag>\n"; } return($fh); } __DATA__ {SOURCE} 0904230634 {DATE} 090424 {EDITION} 1 {HEADLINE} heredero del famoso deportista mexicano, lucha por enaltecer la vida y + obra del autor de sus dM-mas {SOURCE1} Por Gisela Orozco 312.527.8461/ Chicago\ <byline> Por Gisela Orozco< ttl>312.527.8461/ Chicago</ttl>
      When executed, it creates a file called 0904230634.xml in the current directory. The output is
      <?xml version="1.0" encoding="UTF-8"?> <ROOT> <SOURCE>0904230634</SOURCE> <DATE>090424</DATE> <EDITION>1</EDITION> <HEADLINE>heredero del famoso deportista mexicano, lucha por enaltecer + la vida y obra del autor de sus dM-mas</HEADLINE> <SOURCE1>Por Gisela Orozco 312.527.8461/ Chicago {byline}Por Gisela Or +ozco{ttl}312.527.8461/ Chicago</SOURCE1> </ROOT>
      So Instead of
      <?xml version="1.0" encoding="UTF-8"?> <ROOT> <SOURCE>0904230634</SOURCE> <DATE>090424</DATE> <EDITION>1</EDITION> <HEADLINE>heredero del famoso deportista mexicano, lucha por enaltecer + la vida y obra del autor de sus dM-mas</HEADLINE> <SOURCE1>Por Gisela Orozco 312.527.8461/ Chicago</SOURCE1> <byline>Por Gisela Orozco</byline> <ttl>312.527.8461/ Chicago</ttl> </ROOT>
      So please tell me why the string is replaced only with {} and further it is not converted to < as for other tags
        Please tell the name of this format. What is the name of this format?
Re: replace string
by Anonymous Monk on Aug 19, 2009 at 13:09 UTC
    What is the name of this format?
Re: replace string
by sanku (Beadle) on Aug 21, 2009 at 11:45 UTC
    hi, Try out this one.
    open(DATA,"sample.txt") or die $!; while( $line=<DATA> ) { $line=~s/<(\w+)>/\{$1\}\n/g; $line=~s/<\s(\w+)>/\n\{$1\}\n/g; $line=~s/<\/\w+>//g; print $line; }