我试图使用python用逗号分隔字符串:
s = "year:2020,concepts:[ab553,cd779],publisher:elsevier"但我想忽略括号[]中的任何逗号。因此,上述结果将是:
["year:2020", "concepts:[ab553,cd779]", "publisher:elsevier"]有人对怎么做有建议吗?我试着像这样使用re.split:
params = re.split(",(?![\w\d\s])", param)但它并没有正常运作。
发布于 2022-01-12 16:22:33
此正则表达式适用于您的示例:
,(?=[^,]+?:)在这里,我们使用正前瞻查找逗号,后面是非逗号和冒号字符,然后是冒号。这将正确地找到您正在搜索的<comma><key>模式。当然,如果允许键中有逗号,则必须进一步修改。
您可以查看regexr 这里。
发布于 2022-01-12 16:07:38
result = re.split(r",(?!(?:[^,\[\]]+,)*[^,\[\]]+])", subject, 0), # Match the character “,” literally
(?! # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
(?: # Match the regular expression below
[^,\[\]] # Match any single character NOT present in the list below
# The literal character “,”
# The literal character “[”
# The literal character “]”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
, # Match the character “,” literally
)
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[^,\[\]] # Match any single character NOT present in the list below
# The literal character “,”
# The literal character “[”
# The literal character “]”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
] # Match the character “]” literally
)更新以支持括号中的两个以上项目。例如。
year:2020,concepts:[ab553,cd779],publisher:elsevier,year:2020,concepts:[ab553,cd779,xx345],publisher:elsevier发布于 2022-01-12 16:14:03
您可以使用用户定义的函数来解决这个问题,而不是拆分:
s = "year:2020,concepts:[ab553,cd779],publisher:elsevier"
def split_by_commas(s):
lst = list()
last_bracket = ''
word = ""
for c in s:
if c == '[' or c == ']':
last_bracket = c
if c == ',' and last_bracket == ']':
lst.append(word)
word = ""
continue
elif c == ',' and last_bracket == '[':
word += c
continue
elif c == ',':
lst.append(word)
word = ""
continue
word += c
lst.append(word)
return lst
main_lst = split_by_commas(s)
print(main_lst)运行上述代码的结果:
['year:2020', 'concepts:[ab553,cd779]', 'publisher:elsevier']https://stackoverflow.com/questions/70684603
复制相似问题