问题
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"
回答1:
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.
来源:https://stackoverflow.com/questions/15321425/php-preg-match-any-character-except-word