Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How to split line of text into array containing macros and non-macro text

by hazylife (Monk)
on Mar 25, 2014 at 19:22 UTC ( [id://1079728]=note: print w/replies, xml ) Need Help??


in reply to How to split line of text into array containing macros and non-macro text

This will probably do the trick:
#!/usr/bin/perl -nl use strict; my @fields = split /((?:<[^>]+>|\[[^]]+])+)/; shift @fields if $fields[0] eq ''; print for @fields;
  • Comment on Re: How to split line of text into array containing macros and non-macro text
  • Download Code

Replies are listed 'Best First'.
Re^2: How to split line of text into array containing macros and non-macro text
by bulrush (Scribe) on Mar 26, 2014 at 10:43 UTC
    Thank. This seems to work, but I have to hit the ENTER key when I run my test program, to see the output from the print statement. I'd like to make this a subroutine. Test program:
    #!/usr/bin/perl -nl use strict; my($i,$t); $t="<macro1>[tag1]This is plain text.<macro2>More text.[tag2]And more +text."; my @fields=split /((?:<[^>]+>|\[[^]]+])+)/, $t; shift @fields if $fields[0] eq ''; print for @fields; exit;
    Why do I have to hit ENTER to execute the print statement?
      I have to hit the ENTER key when I run my test program
      Because you called Perl with -n which makes it read from input. See perlrun - how to execute the Perl interpreter for details.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: How to split line of text into array containing macros and non-macro text
by bulrush (Scribe) on Mar 26, 2014 at 11:08 UTC
    Oops. Unfortunately my original post had a typo. Tags should be {tag} with braces, not brackets. And I can't seem to decipher your code to replace the brackets with braces. I only see one escaped bracket, which doesn't make sense to me.
      sub parse { my @fields = split /((?:<[^>]+>|{[^}]+})+)/, shift; shift @fields if $fields[0] eq ''; return @fields; }
        Thank you, it worked! I forgot braces do not need to be escaped. I appreciate the examples, they help me learn. I'm more of a visual person.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-03-30 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found