Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match "war" but not "software"

Tags:

regex

grep

What regular expression would match an occurrence of the string "war" that doesn't have the string "soft" in front of it? In other words the "war" in "world war iii" would match, but the "war" in "where's my software" would not? Further, "stay out of my warehouse" would match, so would "aware". In other words, I just don't want the string "software" to match.

like image 977
John Fitzpatrick Avatar asked Dec 05 '25 14:12

John Fitzpatrick


1 Answers

If your regex engine supports lookbehinds: (?<!soft)war.

You can try it using Perl:

$ perl -ne 'print if /(?<!soft)war/i' < my-text-file

That will work more or less like grep (but grep does not support lookbehinds, afaik).

like image 69
Theo Avatar answered Dec 08 '25 08:12

Theo