4

To match any character except specified character (in this example "f") I use [^f]+.

But how to match any character except specified word? Something like [^word]+

Example:

haystack: "bla1 bla2 bla3 bla4 hello bla5 bla6 bla7" (haystack contains HTML and newlines)

needle: "bla1 bla2 bla3 bla4 "

So I want to catch everything from start untill "hello"

Martin
  • 1,312
  • 1
  • 15
  • 18
  • Have you tried.. you know.. just doing the [^word]? – christopher Mar 10 '13 at 10:54
  • This will match anything what contains letters w,o,r,d... – Martin Mar 10 '13 at 10:55
  • you are looking for [Assertion](http://php.net/manual/en/regexp.reference.assertions.php) –  Mar 10 '13 at 10:55
  • Hope it will be easier to do than this name suggest! I am checking mario link. – Martin Mar 10 '13 at 10:56
  • @Martin, your question was correctly closed, but as it stands I think for the wrong reasons. I can not judge if the linked questions are really duplicates (but it seems some people without rep in regex are able to!), since you provide not enough information in your question, to my opinion it should have been closed as not a real question. You should have invested more time into your question, you showed that you have some idea about regex, what is missing are example strings, what you want to match and what not. – stema Mar 10 '13 at 11:54
  • 1
    Your new example doesn't need a regex - it's a simple "is this string in the haystack" - try adding a _real_ example. – AD7six Mar 10 '13 at 20:40

1 Answers1

6

The linked possible duplicate is about matching a row that does not contain a specified word. I am not sure if it is, what is asked here.

If you want to match everything in a string but not the specified word you have to use the anchor \b word boundary instead of ^ start of a string and $ end of a string.

For example

\b(?:(?!your).)+\b

to match everything except the word "your"

See it here on Regexr

(?!your) is a negative lookahead assertion that is true, if the string "your" is not following the current position.

stema
  • 90,351
  • 20
  • 107
  • 135