As long as you have to think about regexes anyway, you can use a regex that parses your text at the same time that it's splitting it.
my $LINE_PATTERN = qr{ ([^:]+) # capture everything before ... :\s* # the colon and any newline or other whitespace, ([^\n]+) # then capture everything before \n # the next newline }msx; my $app_text = "one:partridge\ntwo:\nturtle doves\nthree:french hens\n +"; while ($app_text =~ /$LINE_PATTERN/g) { print "$1: $2\n"; }
If you were planning to put the fields in a hash, you can do it all at once:
my %value_of = $app_text =~ /$LINE_PATTERN/g; while (my ($field, $value) = each %value_of) { print "$field: $value\n"; }
In reply to Re: Pattern match for split() - need match but not match syntax
by Narveson
in thread Pattern match for split() - need match but not match syntax
by Tanoti
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |