首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Regexp模式用于删除括号旁边的空格,并用单个逗号替换括号内单词/字符之间的任何空格。

Regexp模式用于删除括号旁边的空格,并用单个逗号替换括号内单词/字符之间的任何空格。
EN

Stack Overflow用户
提问于 2022-11-07 06:41:29
回答 4查看 69关注 0票数 1

我有类似格式的字符串

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模式,删除括号旁边的任何空格,并用单个,替换括号内单词/字符之间的任何空格。

怎样才能创造出这样的模式?

我目前的尝试是一连串的正则表达式替换。

代码语言:javascript
运行
复制
m = re.sub('\[\s+','[',s)
m = re.sub('\s+\]',']',m)
m = re.sub('\s+',' ',m)
m = re.sub(r'\s(?=[^\[\]]*])', ",", m)

但是,有没有人对如何使它更高效或更清洁有任何建议呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-11-07 23:08:00

您可以在单个捕获组中使用否定字符类,然后用组1中的单个逗号替换1个或多个空格,并将结果封装在方括号中。

代码语言:javascript
运行
复制
\[\s*([^][]*?)\s*]

模式匹配:

  • \[匹配[
  • \s*匹配可选的前导空格字符
  • ( Capture group 1
    • [^][]*?可选择地重复[]以外的字符,尽可能少。

  • )闭组1
  • \s*
  • ]匹配

请参见带有捕获组值的regex演示Python演示

代码语言:javascript
运行
复制
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))

输出

代码语言:javascript
运行
复制
hello this is an example [a,b,c]
hello this is another example [cat,bird,dog,elephant]
票数 2
EN

Stack Overflow用户

发布于 2022-11-07 07:55:12

我没能用一个花哨的模式来做,但是这个小小的解决办法怎么样?只需编写一个在括号之间查找所有内容的模式,然后分别处理该字符串。比如:用空格分隔它,过滤空元素(从开始和结束时的空格开始和尾随),然后将它作为一个由逗号分隔的字符串连接起来。传递给re.sub的修改后的字符串,并将其替换为括号中的所有内容。

代码语言:javascript
运行
复制
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)
)

输出:

代码语言:javascript
运行
复制
hello this is an example [a,b,c]
hello this is another example [cat,bird,dog,elephant]
票数 1
EN

Stack Overflow用户

发布于 2022-11-07 07:57:22

在第一步中,您可以尝试提取方括号之间的文本。代码应该看起来更易读..。

代码语言:javascript
运行
复制
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))  

产出:

代码语言:javascript
运行
复制
hello this is another example [cat,bird,dog,elephant]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74342830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档