Gaurav Soni
Gaurav Soni

Reputation: 6336

Regex to match a string only if a string does not exist

Can anyone suggest me a solution in Regular Expression, to match a string if it does not exist in the given string.

Suppose I have a String

Rohan is going to Home

and I don't want that

Going to

string should exist, then it will not return. But if the string does not contain contains "going to", then string will be returned

Valid

Invalid

I have heard that regex isn't well suited to negate something (except a single character). Regex is more intended to match strings, not "not match" them. Still if someone have solution please suggest in Regular Expresion

I have tried to create a Regular Expression,but didn't get success till now.

SELECT   1 
  FROM   DUAL 
 WHERE   REGEXP_LIKE (' Rohan is 12 home'
                       , '^\s[^going to])$','i'); 

Upvotes: 4

Views: 7642

Answers (2)

Bohemian
Bohemian

Reputation: 425033

Try this, which uses a negative look ahead:

^(?!.*going to.*$)

Upvotes: 4

Maritim
Maritim

Reputation: 2159

Check out negative lookaheads.

Upvotes: 0

Related Questions