这是一种可能性:
(?=[&|=\s])[&|\s]*(?:=[^|]*?[|][&|=\s]+)?
或者在带有解释的自由间距模式下:
(?=[&|=\s]) # make sure there is at least a single separator character ahead;
# this is important, otherwise you would get empty matches, and
# depending on your implementation split every non-separator
# character apart.
[&|\s]* # same as before but leave out =
(?: # start a (non-capturing) group; it will be optional and treat the
# special =...| case
= # match a literal =
[^|]*? # match 0 or more non-| characters (as few as possible)
[|] # match a literal | ... this is the same as \|, but this is more
# readable IMHO
[&|=\s]+ # and all following separator characters
)? # make the whole thing optional
试试看。
编辑:
我刚刚意识到,这吞噬了中心部分,但你也想归还它。在这种情况下,您最好使用匹配而不是拆分(使用find
)。这种模式应该可以解决问题:
=[&|=\s]*([^|]+?)[&|=\s]*[|]|([^&|=\s]+)
现在,第一个或第二个捕获组将包含所需的结果。这是一个解释:
#this consists of two alternatives, the first one is the special case
= # match a literal =
[&|=\s]* # consume more separator characters
([^|]+?) # match 1 or more non-| characters (as few as possible) and
# capture (group 1)
[&|=\s]* # consume more separator characters
[|] # match a literal |
| # OR
([^&|=\s]+) # match 1 or more non-separator characters (as many as possible)
# and capture (group 2)
试试看。