Merry Christmas fellow monks!
For a beanshell (yes! It's not Perl) macro I need a regular expression for tokenizing XML.
I've read several nodes here about not to parse XML using regular expressions. But since I don't want to parse it just to tokenize all the parts of an XML file in a String, I thought it might be a good idea to ask for your assistance.
The regular expression I have now (see below) is sufficiant for the XML in question. But if it's not too much overhead, I'd love to be able to tokenize any valid XML part with it. Or, to be specific, just tags, comments, CDATA, and prolog. I don't care for entities or any DTD.
The expression I have now is: (split up for readability)
(?s)
(?:
(<\w+
(?:\s*\b\w+=
(?:
"[^<"]*"
|
'[^<']*'
)
)*
\s*/?>
)
|
(<!--.*?-->)
|
(<!\[CDATA\[.*?\]\]>)
|
(<!\w+.*?>)
|
(<\?xml.*?\?>)
|
(</\w+>)
)
If this matches, one of these back references is not empty:
- Element
- Comment
- CDATA
- DOCTYPE (etc.?)
- Prolog
- End tag
Maybe some XML expert monks sees some bug in my regex or you see enhancements I could include?
Many thanks in advance!
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
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.