in reply to perl regex to match newline followed by number and text

I am having trouble understanding the problem statement. You data was not enclosed in <code>..</code> tags and that is a problem. I don't really understand what removing the newlines in the 4th field means?

This is just a wild guess on my part - I guess that extra new lines meant spacers between these records - but maybe not?:

use strict; use warnings; my $line; while (<DATA>) { chomp; if ( (/:$/../^\s*$/) =~ /^\d+$/) #exclude endpoint. { s/\s,\s/,/; $line .= " $_"; } elsif (defined $line) { $line =~ s/^\s*//; print "$line\n"; $line = undef; } } print "$line\n" if defined $line; # just to be sure # all output is done =prints 99~Arun~Kumar~Mobilenum: 1234-567,from Earth Human 98~Mahesh~Babu~Mobilenum: 5678-901,from Earth Human 98~Mahesh~Babu~Mobilenbbb: 5678-901,from Earth Human =cut __DATA__ 99~Arun~Kumar~Mobilenum: 1234-567 , from Earth Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth Human 98~Mahesh~Babu~Mobilenbbb: 5678-901 , from Earth Human
I guess something very, very simple like this is possible? 3 input lines to one output line?
use strict; use warnings; my $input = do {local $/; <DATA>}; my @lines = $input =~ m/(.*\n.*\n.*\n)/g; foreach my $line (@lines) { $line =~ s/\n/ /g; $line =~ s/ , /,/g; print "$line\n"; } =Prints 99~Arun~Kumar~Mobilenum: 1234-567,from Earth Human 98~Mahesh~Babu~Mobilenum: 5678-901,from Earth Human 98~Mahesh~Babu~Mobilenbbb: 5678-901,from Earth Human =cut __DATA__ 99~Arun~Kumar~Mobilenum: 1234-567 , from Earth Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth Human 98~Mahesh~Babu~Mobilenbbb: 5678-901 , from Earth Human

Replies are listed 'Best First'.
Re^2: perl regex to match newline followed by number and text
by holli (Abbot) on Jun 01, 2019 at 11:57 UTC
    I guess something very, very simple like this is possible? 3 input lines to one output line?
    Good idea, but why then still use a regex?
    while (<DATA>) { chomp; print; print $. % 3 ? " " : "\n"; } __DATA__ 99~Arun~Kumar~Mobilenum: 1234-567 , from Earth Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth Human
    Output
    99~Arun~Kumar~Mobilenum: 1234-567 , from Earth Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth Human


    holli

    You can lead your users to water, but alas, you cannot drown them.
      The number of newlines is not fixed, it might be one or two or three or more newlines in the last field
        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.