For my daily installment of "I'm doing something dumb, I can just feel it" I present the following conundrum to the helpful Monks:
I am working with some XML, and I have several instances of lines like these:
<heading>
level=2,
Introduction to Arguments</heading>
or
<index>
primary-key="listed-arguments",
secondary-key="passing-functions",
<paragraph>
Arguments allow you to pass information to functions.
There are two categories of arguments:
</paragraph>
</index>
In these snippets, I'd like to turn the
<foo> something=bar, Code is cool </foo>
into
<foo something=bar> Code is cool </foo>
that is, turn the something=bar into an attribute of the tag. So in order to do this, I implemented this regex:
$text =~ s/>\s?([\w\-]*?)\=([\w\-\"]*?)\,?/ $1\=$2>/gixs;
which, in my mind anyway, looks for the tag ending > followed by an optional space, then a "word"(plus numerals)/dashes followed by a mandatory equals sign followed by another word/dash/quotation mark combination followed by an optional comma. If the space or comma are found, they're eaten. The other stuff is replaced as an attribute.
Well, if it was working I wouldn't be asking, would I? :-)
So if anyone can give me a hint as to where my regex goes wrong, I'd appreciate it a lot. I have to buy that "mastering regular expressions" book and get it over with :-) Anyway, any and all help appreciated. The folks on this board are probably my most invaluable perl tool......thanks Monks!
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.