-2

Is there any regex like this:

"/Hello my name is [ANYTHING THAT IS NOT "john"], and I like pizza/"

So when I match with this regex, it should return true as long as the place where the name is is not "john".

"Heloo my name is abcd, and I like pizza" --> false
"Hello my name is asdf, and I like pizza" --> true
"Hello my name is john, and I like pizza" --> false

What should my regex look like to do this in the best way?

3 Answers3

2
Hello\smy\sname\sis\s(?:(?!\bjohn\b).)*?\sand\sI\slike\spizza

Try this.See demo.

https://regex101.com/r/iY3eK8/14

vks
  • 67,027
  • 10
  • 91
  • 124
2

Try this.

/Hello my name is (?!john)[^,]+, and I like pizza/
  • Cool, but what does that [^,]+ mean? –  Dec 09 '14 at 16:00
  • `[^,]+` means one or more characters that isn't a comma. – Corey Ogburn Dec 09 '14 at 16:07
  • @Murplyx - `Cool, but what does that [^,]+ mean?` Its a character class, says not a comma chracter, quantified 1 to many times. You can restrict it so it doesn't go past a newline by `[^,\n]+`. –  Dec 09 '14 at 16:10
0

The one through PCRE verb (*SKIP)(*F)

Hello my name is john, and I like pizza(*SKIP)(*F)|Hello my name is [^,]+, and I like pizza

|<----String you don't want----------->|<-VERB--->|<------String you want----------------->

DEMO

Community
  • 1
  • 1
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274