我期待看看是否有一種方法來獲得一個匹配組的每個逗號分隔列表後積極lookbehind。積極lookbehind其次逗號分隔列表
例如
#summertime swimming, running, tanning
正則表達式(到目前爲止)
(?<=#summertime\s)(.+)
返回
["swimming, running, tanning"]
期望的結果
["swimming", "running", "tanning"]
我期待看看是否有一種方法來獲得一個匹配組的每個逗號分隔列表後積極lookbehind。積極lookbehind其次逗號分隔列表
例如
#summertime swimming, running, tanning
正則表達式(到目前爲止)
(?<=#summertime\s)(.+)
返回
["swimming, running, tanning"]
期望的結果
["swimming", "running", "tanning"]
的傳統方式在PCRE/perl的解決這個問題是使用\K
escape sequence和\G
anchor:
(?: # non-capturing group
\#summertime\b # match #summertime
| # or
\G(?<!^), # a comma not at the beginning of string and match it only if it's after the last match
) # closing the non-capturing group
\s* # some optional whitespaces
\K # forget what we matched so far
[^\s,]+ # match anything that's not a whitespace nor a comma one or more times
有關正則表達式的一些說明:
x
修改器進行白色間距模式。g
修飾符進行全部匹配。在PHP中,您將需要使用preg_match_all()
。\G(?<!^)
是從最後一點開始匹配的經典方法,而不是從字符串/行的開頭進行匹配。你也可以用這種形式看到它\G(?!^)
或(?!^)\G
。請記住,這全是零寬度。\K
是awesome。[^\s,]+
,但你也可以使用\w+
或什麼都適合你的需要。\s*,\s*
我想你最好還是分割字符串你目前與'得到的,'分裂。 – NigoroJr 2014-08-28 05:42:31
你在用什麼語言? – Toto 2014-08-28 07:19:11