preg_match_all 匹配可选括号
preg_match_all match optional brackets
正在尝试 preg_match_all
#THING or [#THING]
这适用于第一个
/(#[A-Z]+)/
对于另一个带括号的,我认为这样的事情应该有效,但它没有。
/(\[?#[A-Z]+\]?)/
最终,我想匹配其中任何一个
#THING [(#THING)] or [anything(#THING)anything]
编辑:这似乎对我有用。
((\[(.*))?\(?#[A-Z]+\)?((.*)\])?)
正确匹配这些:
#ANYUPPERCASE
[(#ANYUPPERCASE)]
[anything(#ANYUPPERCASE)anything]
这似乎是一个 robust/reliable 模式:
~#[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" 子字符串[]
也可能在 (
之前或 )
之后包含一些字符。
这种模式是有效的,因为它使用贪婪量词并且不使用任何捕获组。
正在尝试 preg_match_all
#THING or [#THING]
这适用于第一个
/(#[A-Z]+)/
对于另一个带括号的,我认为这样的事情应该有效,但它没有。
/(\[?#[A-Z]+\]?)/
最终,我想匹配其中任何一个
#THING [(#THING)] or [anything(#THING)anything]
编辑:这似乎对我有用。
((\[(.*))?\(?#[A-Z]+\)?((.*)\])?)
正确匹配这些:
#ANYUPPERCASE
[(#ANYUPPERCASE)]
[anything(#ANYUPPERCASE)anything]
这似乎是一个 robust/reliable 模式:
~#[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" 子字符串[]
也可能在 (
之前或 )
之后包含一些字符。
这种模式是有效的,因为它使用贪婪量词并且不使用任何捕获组。