Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match specific strings without a given prefix

Tags:

regex

I need to match all of its lines that contains a value and that don't have a given prefix.

Example: I want all lines that contains word when it's not prefixed by prefix

So:

foobar -> no match prefix word -> no match prefix word suffix -> no match word -> MATCH something word -> MATCH 

What I've tried so far:

(?!prefix)word 

Doesn't seem to do what I want

like image 892
Guillaume Avatar asked May 28 '12 08:05

Guillaume


People also ask

How do you match a character except one regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).

What is \r and \n in regex?

\n. Matches a newline character. \r. Matches a carriage return character.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.


1 Answers

You may need

(?<!prefix )word 

(and maybe take care of the spaces).

(?!) is a negative lookahead but in your case you need a negative lookbehind (i.e. (?<!)).

like image 152
Howard Avatar answered Sep 24 '22 21:09

Howard