Hi

I am trying to implement a function to split a sentence with XML tags in 3 parts. To be more clear, I want to parse a sentence with XML tags so that I will not have the wrapping tags. Input sentences are:

<d id="43">Text </d> here <a id="33"/> <b id="33"/> Text <d id="43">text</d> here <d id="43">text here</d> <d id="43">text here</d> <d id="44">text here</d>

Output should be

start: "", middle: "<d id="43">Text </d> here", end: " <a id="33"/>" start: "<b id="33"/> ", middle: "Text <d id="43">text</d> here", end: +"" start: "<d id="43">", middle: "text here", end: "</d>" start: "", middle: "<d id="43">text here</d> <d id="44">text here</d>" +, end: ""

I started my code but I don't think it's efficient. Any suggestions? a, b and c tags are self tags while d is always paired.

my $segment = $_; my $start =""; my $end =""; my $middle =""; while ($segment =~ /^(<[a|b|c] id=\".*?\"\/>)/ || $segment =~ /^(\ +s+)/){ $start .= $1; $segment =~ s/^\Q$1\E//; } while ($segment =~ /(\s+)$/ || $segment =~ /(<[a|b|c] id=\".*?\"\/ +>)$/){ $end = "$1$end"; $segment =~ s/\Q$1\E$//; } while ($segment =~ /^(<d id=\".*?\">).*?(<\/d>)/){ ---- } print "start: \"$start\", middle: \"$middle\", end: \"$end\"\n";

In reply to Getting start and end xml tags by corfuitl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.