Help: Regular Expression for Negate Matching String

Hi guys,

as per subject I am having problem with regular expressions.

Example, if i got a string "javax.servlet.http.HttpServlet.service" that may occurred anywhere within a text file.

How can I used the negate pattern matching of regular expression?

I tried the below pattern but it throws a syntax error.
# egrep '^*(?!javax.servlet.http.HttpServlet.service)*$' test.txt

Appreciate if anyone can help.

Thanks in advance.

fgrep -v 'javax.servlet.http.HttpServlet.service' myFile
OR
sed '/javax.servlet.http.HttpServlet.service/d' myFile
OR
nawk '!/javax.servlet.http.HttpServlet.service/' myFile

Do you mean

  1. find out if it ever occurs in the file
grep -q 'javax\.servlet.http\.HttpServlet\.service' somefile.txt
if [[ $? -ne 0 ]] ; then
 echo "file is okay - no string found"
fi
  1. print lines that do not have the pattern
grep -v 'javax\.servlet.http\.HttpServlet\.service' somefile.txt

Otherwise please explain what you want, not how you wanted to do it. A negated character class (which what you wrote is not an example of) means 'match anything else except this'. grep -v [pattern] has this meaning.

Hi guys,

thanks for the examples.

I am actually looking for a regular expression to search for a string in a text file; if throughout the file no such string it finds occur, it will throw out an error.

Maybe i am looking at the wrong direction but bottom line is i need this to be regular expression and I may not be using the standard unix command to do this. :frowning:

Conversely, regular expressions by themselves do one thing: find strings using pattern matching.

Again - what are you trying to do? If I remember correctly, ! negates a perl regex.
But what negation means: it still finds everything else. It returns the whole string only if the pattern match fails. Is that what you want?

In truth your question does not make a lot of sense to me.