in reply to Re: grep only lines having matched pattern
in thread grep only lines having matched pattern

Well since \n is a space character - see below. However it appears to me that anchoring this regex to the beginning of the line is just fine.
use strict; use warnings; while (<DATA>) { if (/\d{2}-\d{2}-\d{4}\s+/) { print; } } =Prints: 03-15-2021 21.1.0-s102 2021/03/15:04:00:09 21.1 21.10-s102 21.1.0-s102 2021/03/15:04:00:09 21.1 21.10-s102 03-15-2021 **works** =cut __DATA__ 03-15-2021-1 21.1.0-s103 2021/03/15:14:16:39 21.1 21.10-s103 03-15-2021-2 21.1.0-s103 2021/03/15:14:16:39 21.1 21.10-s103 03-15-2021 21.1.0-s102 2021/03/15:04:00:09 21.1 21.10-s102 21.1.0-s102 2021/03/15:04:00:09 21.1 21.10-s102 03-15-2021 21.1.0-s102 2021/03/15:04:00:09 21.1 21.10-s102 03-15-2021-4
I guess that /\d{2}-\d{2}-\d{4}[^-]/would also work?

Replies are listed 'Best First'.
Re^3: grep only lines having matched pattern
by Tux (Canon) on Apr 02, 2021 at 07:43 UTC

    Using DATA in example code tells me nothing about the *real* source for the data. It can be a log file or a database or a process that pipes otther sources into a (stream of) single lines of log that have no line endings at all.

    To *me* thinking out of that box has caused me to sometime be overprotective and think out of the box. It not only makes many lines in my code show more explicit what the intent is, but it also protects against the other ways in what this data can be supplied (in the future).

    Be liberal on the recieving end and be strict on the producing end.

    Been there, done that: you have no idea how completely valid CSV files get corrupted by people in the chain that want to "check" the content using a spreadsheet program like Excel and instead of exiting hit "OK" when the program asks them to write the changed data even if the change is just widening the column or changing the font.


    Enjoy, Have FUN! H.Merijn