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?).

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re: perl regex to match newline followed by number and text by GrandFather
in thread perl regex to match newline followed by number and text by arunkumarzz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.