in reply to Re^2: perl regex to match newline followed by number and text
in thread perl regex to match newline followed by number and text

The number of newlines is not fixed, it might be one or two or three or more newlines in the last field
  • Comment on Re^3: perl regex to match newline followed by number and text

Replies are listed 'Best First'.
Re^4: perl regex to match newline followed by number and text
by holli (Abbot) on Jun 01, 2019 at 18:29 UTC
    it might be one or two or three or more newlines in the last field
    Well it'd be nice if your sample data would reflect that. Anyhow. I'd solve this using a simple algorithm rather than a complicated regex that backtracks god knows how often with all these .*
    Consider:
    use warnings; use strict; my @record; while (<DATA>) { if (/^\d+\~/ && @record) { print join (" ", @record), "\n"; @record = (); } chomp; push @record, $_; } print join (" ", @record), "\n"; __DATA__ 99~Markus~Holli~Mobilenum: 1234-567 , from Earth Europe White Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth India Brown Human
    Output
    99~Markus~Holli~Mobilenum: 1234-567 , from Earth Europe White Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth India Brown Human


    holli

    You can lead your users to water, but alas, you cannot drown them.