select\[([^\s]*(?<param>[a-z0-9]+)[^,\s]*)*\]
尝试从逗号分隔的参数加载捕获组(数字或字母数字选项),忽略每个参数的前导/尾随空格,但保留单词之间的空格(即“两个单词”)。
select[ 1, 22 ,word, two words ]
成为:
param1: "1"
param2: "22"
param3: "word"
param4: "two words"
一旦排序,想要处理参数周围的可选单引号'。
感谢您的考虑
最佳答案
另一种用于支持 \G
的引擎:
(?:\G(?!\A)|select\[) # look for the last match or select[
\s* # whitespaces, optional and greedy
((?:(?!(?:[ ]{2,}|\]|,)).)+) # not overrunning two consecutive spaces, ] or ,
\s* # another greedy whitespace
(?:,|\]) # , or ]
劫持您的演示: https://regex101.com/r/a0ab0Q/8
Python
):
import regex as re
rx = re.compile(r'''
(?:\G(?!\A)|select\[)
\s*
((?:(?!(?:[ ]{2,}|\]|,)).)+)
\s*
(?:,|\])
''', re.VERBOSE)
params = [match.group(1).strip()
for match in rx.finditer(string)]
print(params)
# ['1', '22', 'word', 'two words']
关于python - 正则表达式匹配函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43708719/