Positive lookbehind followed by a comma separated list
I am looking to see if there is a way to get the match group for each of the comma separated lists after a positive search.
for example
#summertime swimming, running, tanning
Regex (bye)
(?<=#summertime\s)(.+)
Returns
["swimming, running, tanning"]
Desired results
["swimming", "running", "tanning"]
+3
source to share
2 answers
The classic way to solve this problem in PCRE / perl is to use an \K
escape sequence and \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 after the last match
) # closing the non-capturing group
\s* # some optional whitespaces
\K # forget what we matched so far
[^\s,]+ # match anything that not a whitespace nor a comma one or more times
Some notes on regex:
- I used a modifier
x
for white spacing mode. - You may need to use a modifier
g
to match all depending on the language. In php, you will need to usepreg_match_all()
. - I have avoided hashtags in
#summertime
because hashtag is for white spaced comments. -
\G(?<!^)
- the classic way of matching with the last point, not from the beginning of the line / line. You can also see it in this form\G(?!^)
or(?!^)\G
. Remember that they are all zero-width. -
\K
awesome . - I have used
[^\s,]+
, but you could use\w+
or whichever suits your needs. - A bit late, but you can also use your own solution and then split by
\s*,\s*
+1
source to share