Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Split tags and words nicely

by themage (Friar)
on Dec 28, 2006 at 13:18 UTC ( #592035=note: print w/ replies, xml ) Need Help??

in reply to Split tags and words nicely

Hi bwgoudey,

I think you may be looking for this:

$a=q{<tag ref=1>Start<tag ref=2>and more</tag>and end</tag>}; @l=split qr{(</?tag[^>]*>)}, $a; print join "\n", @l;
The main trick is to use () inside the regex used in split to capture the delimiters.

Comment on Re: Split tags and words nicely
Select or Download Code
Replies are listed 'Best First'.
Re^2: Split tags and words nicely
by logie17 (Friar) on Dec 28, 2006 at 21:04 UTC
    Along the same lines, here's something that is completely regex and no join or split is needed. Probably could be obfuscated even more :)
    $a = q{<tag ref=1>Start<tag ref=2>and more</tag>and end</tag>}; my @b; $a =~ s/(<\/?tag[^>]*>)(\w*)/push @b, ($1,$2)/eg; map {print $_,"\n"} @b;

    Prints the following:
    <tag ref=1> Start <tag ref=2> and </tag> and </tag>

    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
      You have a slight glitch in that you are losing any text after the space, e.g. "and more" comes out as "and". Fix:

      $a =~ s/(<\/?tag[^>]*>)([\w ]*)/push @b, ($1,$2)/eg;

      Also it is probably a good idea to avoid $a and $b for variable names because of their special status with regard to sort.



        Doh! Nice catch. Good call with $a, $b.

        s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://592035]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2015-11-29 18:04 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (752 votes), past polls