Hello Monks! I've got a problem that I'm sure has a simple solution yet my terrible (read non-existant) grasp of regexes makes a real struggle. I need to split some text that contains a tilda up with a line return.
Example:
Data In:
AAA~BB~CCCCC~DDD~
Data Out:
AAA
BB
CCCCC
DDD
Which worked fine in the past with this HORRIBLE code I wrote long ago:
sub add_line_break
{
my (@out)=@_;
for (@out) {
s/~+/\n/;
s/~+/\n/;
s/~+/\n/;
s/~+/\n/;
...
}
return wantarray ? @out :$out[0];
}
Now I need to do the same thing but leave the tilda on the end. The lines have varying numbers elements in them so I'd love it to do it for everyone of them without me having to duplicate the regex (which I'm sure I only had to do because of my poor grasp.
Example:
Data In:
AAA~BB~CCCCC~DDD~
Data Out:
AAA~
BB~
CCCCC~
DDD~
Can someone help me out. I'm hoping some real world examples will help me grasp regexs finally. I've struggled with them for YEARS. Tried different books and sites and I still can't close the loop for some reason.
Thanks as always for your help!!!
Mark
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.