Adam Hughes
Adam Hughes

Reputation: 16319

Regex for GCP log search "not in"

We have GCP logs like

I'd like to find all logs that match this exact pattern except for the name being 'adam'. For example, would only keep Hi my name is 'steve'.

In GCP legacy logs viewer, I've tried the following search to no avail

Hi my name is '[^a]'
Hi my name is '[^adam]'
Hi my name is '^((?!adam).)*$'

Pretty bad at regex in general, and not sure that GCP parses them identically to other https://regexr.com/ default settings that I've been testing on. Am I offbase entirely or should one of these more or less work?

Upvotes: 0

Views: 1835

Answers (2)

Francis Gagnon
Francis Gagnon

Reputation: 3675

You were getting closer with your last attempt. If I understand what you want correctly, you could use one of the following expressions:

Adam surrounded by single quotes:

^Hi my name is '(?!adam)[^']*'

Here's a breakdown of this expression:

Hi my name is '        #Match some static text
(?!adam)               #Ensure the word 'adam' doesn't appear at this point.
[^']*                  #Match all characters until you encounter a single quote.
'                      #Match a single quote.       

Adam without single quotes:

^Hi my name is (?!adam)[^\n]*

Here's a breakdown of this expression:

Hi my name is          #Match some static text
(?!adam)               #Ensure the word 'adam' doesn't appear at this point.
[^\n]*                 #Match all characters until you encounter a newline.   

Upvotes: 3

The fourth bird
The fourth bird

Reputation: 163632

You might optionally match the quotes, and after that check for adam. If the assertion succeeds, then match the rest of the line.

\bHi my name is ["']*\b(?!adam\b).+
  • \bHi my name is Match the string preceded by a word boundary
  • ["']*\b Optionally match a single or double quote followed by a word boundary
  • (?!adam\b) Negative lookahead, assert not adam directly to the right
  • .+ Match 1+ times any character except a newline

Regex demo

Upvotes: 2

Related Questions