2

I am having trouble finding the correct grep expression for not matching entire lines in BBEdit that do not contain a date, despite having found many "match ... not containing" topics on this on the web...

I have this text document:

Some Text
Some more text,even more text,2015-06-17,2015-06-20
A third line of text
Last line of text, 2015-06-17

This expression will select all lines that contain a date reference, in the form of 4 digits + "-" + 2 digits + "-" + 2 digits

^.*\d\d\d\d-\d\d-\d\d.*$

I would like to match exactly the opposite, with the intention to remove all lines that do not contain a date reference. I have tried solutions like

^.*[^\d\d\d\d-\d\d-\d\d].*$

but with no success so far. Can someone point me in the right direction? Thank you.

BMM
  • 550
  • 10
  • 24

2 Answers2

6

BBEdit supports Perl-Style Pattern Extensions (see page 183 of the manual) including negative lookaheads (?!...).

I believe this will do what you want:

^((?![\d\d\d\d-\d\d-\d\d]).)*$
Community
  • 1
  • 1
steveax
  • 16,627
  • 5
  • 41
  • 59
  • Thank you. Worked perfectly. – BMM Jun 14 '15 at 23:25
  • 3
    This certainly does the trick, but could use a bit more explanation. I was trying to do something similar, knew I needed a negative lookahead, but created something like this: `^.*(?![^\d\d\d\d-\d\d-\d\d]).*$` until I found this page. Obviously, I don't fully understand the use of the additional parenthesis and why `.*` isn't needed in the beginning. If anyone can elaborate in the comments or by editing the answer, I'd appreciate it. – D. Woods Sep 30 '16 at 19:39
1

One option

"[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}"

if you want to exclude the lines that dont have this match its easier to use grep -v

PerroVerd
  • 901
  • 9
  • 21
  • Thanks. Your suggestion has made me simplify the expression into this: `[0-9]{4}-[0-9]{2}-[0-9]{2}`. Still, I can't use the -v option since I am using BBEdit specific grep functionality in its search and replace window. – BMM Jun 12 '15 at 16:06