Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore specific lines when matching with a regex

Tags:

regex

I'm trying to make a regex that matches a specific pattern, but I want to ignore lines starting with a #. How do I do it?

Let's say i have the pattern (?i)(^|\W)[a-z]($|\W)

It matches all lines with a single occurance of a letter. It matches these lines for instance:

asdf e asdf
j
kke o

Now I want to override this so that it does not match lines starting with a #

EDIT:

I was not specific enough. My real pattern is more complicated. It looks a bit like this: (?i)(^|\W)([a-hj-z]|lala|bwaaa|foo($|\W)

It should be used kind of like I want to block offensive language, if a line does not start with a hash, in which case it should override.

like image 896
patwotrik Avatar asked Jan 14 '23 05:01

patwotrik


1 Answers

This is what you are looking for

^(?!#).+$

^ marks the beginning of line and $ marks the end of line(in multiline mode)

.+ would match 1 to many characters

(?!#) is a lookahead which would match further only if the line doesn't start with #

like image 180
Anirudha Avatar answered Jan 29 '23 14:01

Anirudha