in reply to perl regex to match newline followed by number and text
Care to show us some sample data and how you'd like the result to look?
Note that you can use (?:...) to group stuff without capturing which can clean things up quite a bit. As a hint, I've added a little white space to your regex to make the groupings more obvious and added a digit at the start of each capture group. Maybe those numbers are not quite what you expect?
s/((\n) ([^0-9])+ (-)* (Aa-Zz)*) | ((\n) (\d{3}) (-)* (Aa-Zz)*)/$2$3/g +x; # 12 3 4 5 67 8 9 0
Update:
Maybe what you want to achieve is something like this:
use strict; use warnings; my $wholeBallOfWax = do {local $/; <DATA>}; my @records = split /(?<=\n)(?=\d+-)/, $wholeBallOfWax; s/\n+$/\n/s for @records; print join "---\n", @records; __DATA__ 1-12 last non-blank field 2-10 data more data 3-21 stuff more stuff Lots of stuff so much stuff there is no following empty field 4-73 Sneeky record with a blank field in the middle! 5-00 Last record
Which prints:
1-12 last non-blank field --- 2-10 data more data --- 3-21 stuff more stuff Lots of stuff so much stuff there is no following empty field --- 4-73 Sneeky record with a blank field in the middle! --- 5-00 Last record
In the split regex there is a look behind ((?<=\n)) which matches a new line before the current search point, and a look ahead ((?=\d+-)) which matches one or more digits followed by a hyphen. Neither match "consumes" the string that was matched so the split doesn't drop any characters.
As an aside, the do {local $/; <DATA>} bit suspends end of line detection and reads everything from <DATA> into $wholeBallOfWax (although maybe that was obvious?).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: perl regex to match newline followed by number and text
by arunkumarzz (Novice) on May 31, 2019 at 15:12 UTC | |
by poj (Abbot) on May 31, 2019 at 15:48 UTC | |
by arunkumarzz (Novice) on Jun 02, 2019 at 17:58 UTC | |
by Marshall (Canon) on Jun 01, 2019 at 06:54 UTC |