Hello Monks.
So I have an xml file that looks like this:
<author order="4">
<name>
<given_name>G. Mileti</given_name>
<surname/>
</name>
</author>
Obviously, it would be better if the given name and the surname were within the correct tags. So, I ran this:
perl -i.xml -p -e "s#<given_name>(\w\.) (\w+)</given_name(>?)#<given_name>$1</given_name>\n<surname>$2</surname$3#ig"
However, that replaces the xml with:
<author order="4">
<name>
<given_name></given_name>
<surname></surname>
<surname/>
</name>
</author>
I'm not concerned with the fact that there are two surname tags. I was planning on the cleaning up the second one anyway. I am concerned that it makes me lose data. The capturing groups don't appear to be capturing.
When I open the file in oXygen (an xml editor) and use its regular expression find and replace option:
Find: <given_name>(\w\.) (\w+)</given_name>
Replace: <given_name>$1</given_name>
<surname>$2</surname>
That works. They are nearly the same code. The only difference being with the closing > on given_name in the perl code to account for the fact that for some reason XML::Simple thinks that having newlines before each closing > is a good idea.
Any help would be greatly appreciated.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.