in reply to replace string

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

Replies are listed 'Best First'.
Re^2: replace string
by vitoco (Hermit) on Aug 19, 2009 at 14:42 UTC

    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.