PHP preg_match any character except word [duplicate]

大兔子大兔子 提交于 2019-12-22 08:40:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!