我有类似格式的字符串
hello this is an example [ a b c ]
hello this is another example [ cat bird dog elephant ]
我想把它转换成
hello this is an example [a,b,c]
hello this is another example [cat,bird,dog,elephant]
但我不明白如何创建regexp模式,删除括号旁边的任何空格,并用单个,
替换括号内单词/字符之间的任何空格。
怎样才能创造出这样的模式?
我目前的尝试是一连串的正则表达式替换。
m = re.sub('\[\s+','[',s)
m = re.sub('\s+\]',']',m)
m = re.sub('\s+',' ',m)
m = re.sub(r'\s(?=[^\[\]]*])', ",", m)
但是,有没有人对如何使它更高效或更清洁有任何建议呢?
发布于 2022-11-07 23:08:00
您可以在单个捕获组中使用否定字符类,然后用组1中的单个逗号替换1个或多个空格,并将结果封装在方括号中。
\[\s*([^][]*?)\s*]
模式匹配:
\[
匹配[
\s*
匹配可选的前导空格字符(
Capture group 1 [^][]*?
可选择地重复[
和]
以外的字符,尽可能少。)
闭组1\s*
]
匹配import re
strings = [
"hello this is an example [ a b c ]",
"hello this is another example [ cat bird dog elephant ]"
]
pattern = r"\[\s*([^][]*?)\s*]"
for s in strings:
print(re.sub(pattern, lambda m: "[{0}]".format(re.sub(r"\s+", ',', m.group(1))), s))
输出
hello this is an example [a,b,c]
hello this is another example [cat,bird,dog,elephant]
发布于 2022-11-07 07:55:12
我没能用一个花哨的模式来做,但是这个小小的解决办法怎么样?只需编写一个在括号之间查找所有内容的模式,然后分别处理该字符串。比如:用空格分隔它,过滤空元素(从开始和结束时的空格开始和尾随),然后将它作为一个由逗号分隔的字符串连接起来。传递给re.sub
的修改后的字符串,并将其替换为括号中的所有内容。
s1 = "hello this is an example [ a b c ]"
s2 = "hello this is another example [ cat bird dog elephant ]"
pattern = r"(?<=\[)(.*)(?=\])"
print(
re.sub(
pattern,
','.join(list(filter(None, re.split(r"\s+", re.search(pattern, s1).group(1)))))
, s1)
)
print(
re.sub(
pattern,
','.join(list(filter(None, re.split(r"\s+", re.search(pattern, s2).group(1)))))
, s2)
)
输出:
hello this is an example [a,b,c]
hello this is another example [cat,bird,dog,elephant]
发布于 2022-11-07 07:57:22
在第一步中,您可以尝试提取方括号之间的文本。代码应该看起来更易读..。
foo = 'hello this is another example [ cat bird dog elephant ]'
# get everything between [ and ]
reg_get_between_square_brackets= re.compile(r'\[(.*)\]')
str_to_replace = reg_get_between_square_brackets.findall(foo)[0]
# replace spaces with coma
new_string = re.sub('\s+', ',', str_to_replace.strip()) # strip to remove beginning/ending white space
print(foo.replace(str_to_replace, new_string))
产出:
hello this is another example [cat,bird,dog,elephant]
https://stackoverflow.com/questions/74342830
复制相似问题