Regex 正查找,后跟逗号分隔的列表
我想看看是否有一种方法可以在肯定的查找之后为每个逗号分隔的列表获取匹配组 比如说Regex 正查找,后跟逗号分隔的列表,regex,Regex,我想看看是否有一种方法可以在肯定的查找之后为每个逗号分隔的列表获取匹配组 比如说 #summertime swimming, running, tanning 正则表达式(至今) 预期结果 ["swimming", "running", "tanning"] 在php中,您可以通过PCRE动词(*SKIP)(*F)来实现这一点 在PCRE/perl中解决此问题的经典方法是使用和: (?:#非捕获组 \#夏季比赛 |#或 \G(?)? 关于正则表达式的一些注释: 我在白间隔模式中使用了x修饰
#summertime swimming, running, tanning
正则表达式(至今)
预期结果
["swimming", "running", "tanning"]
在php中,您可以通过PCRE动词
(*SKIP)(*F)
来实现这一点
在PCRE/perl中解决此问题的经典方法是使用和:
(?:#非捕获组
\#夏季比赛
|#或
\G(?)?
关于正则表达式的一些注释:
- 我在白间隔模式中使用了
x
修饰符
- 根据语言的不同,您可能需要使用
g
修饰符来匹配所有内容。在php中,您需要使用preg\u match\u all()
- 我在
#summertime
中转义了hashtag,因为该hashtag用于白间隔模式下的注释
\G(?)是从最后一点而不是从字符串/行的开头进行匹配的经典方式。您也可以在以下形式中看到它\G(?!^)
或(?!^)\G
。请记住,它都是零宽度
\K
是
- 我使用了
[^\s,]+
,但您也可以使用\w+
或任何适合您需要的工具
- 有点晚了,但您最好使用自己的解决方案,然后按
\s*,\s*
我认为你最好把你现在用,
得到的字符串分开。你用的是什么语言?问题是它甚至会匹配游泳、跑步、晒黑
是否有#夏季
。。。
["swimming, running, tanning"]
["swimming", "running", "tanning"]
(?:^(?:(?!#summertime).)*$|^.*?#summertime)(*SKIP)(*F)|\w+
(?: # 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