尝试preg_match_all
#THING or [#THING]
这适用于第一个
/(#[A-Z]+)/
对于带括号的其他人,我认为这样的事情应该有用,但事实并非如此。
/(\[?#[A-Z]+\]?)/
最终,我想匹配其中任何一个
#THING [(#THING)] or [anything(#THING)anything]
答案 0 :(得分:0)
((\[(.*))?\(?#[A-Z]+\)?((.*)\])?)
正确匹配这些:
#ANYUPPERCASE
[(#ANYUPPERCASE)]
[anything(#ANYUPPERCASE)anything]
答案 1 :(得分:0)
这似乎是一个强大/可靠的模式:
~#[A-Z]+|\[[^#\]]*\(#[A-Z]+\)[^\]]*]~
~ #pattern delimiter
#[A-Z]+ #match hash symbol followed by one or more uppercase letters
| #or
\[ #match an opening square bracket
[^#\]]* #match zero or more non-hash/non-closing square bracket characters
\( #match opening parenthesis
#[A-Z]+ #match hash symbol followed by one or more uppercase letters
\) #match closing parenthesis
[^\]]* #match zero or more non-closing square bracket characters
] #match a closing square bracket
~ #pattern delimiter
这种模式正在寻找一个“hashtag”子字符串,其中没有[()]
个字符或一个“hashtag”子字符串,它将紧紧包裹在()
中,然后松散地包裹在[]
中也可能在(
之前或)
之后包含一些字符。
这种模式很有效,因为它使用贪婪的量词,并且不使用任何捕获组。