如何替换逗号分隔的名称列表中的逗号

2024-04-25 13:21:17 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中,我有一个由逗号分隔的名称字符串,我试图在名称周围添加双方括号。你知道吗

这是原始字符串的格式。
1. John Smith1, John Smith2, John Smith3, etc.<br>

我想要的是:
1. [[John Smith1]], [[John Smith2]], [[John Smith3]], [[etc.]]<br>

我试过使用这个正则表达式:
(.+?)(?:, |(<br>)$)

更换后:
[[\1]], \2

但它给出了这样的结果:
[[1. John Smith1]], [[John Smith2]], [[John Smith3]], [[etc.,]]<br>

  1. 如何将首字母“\d.\s”移到名字捕获之外?你知道吗
  2. 如何防止姓氏后面的最后一个逗号(在本例中是etc,而不是etc.)?你知道吗

任何建议都将不胜感激。你知道吗

更新
我为没有说得更具体而道歉。当我说我想匹配这个,我应该说匹配“只”这个模式。你知道吗

当我使用这个正则表达式:(?<=\.\s|,\s)([^,\r\n]+)\s*(?=<br>|,)和这个替换[[\1]]时,它做了两件意想不到的事情。
1尽管它在regex101.com中工作,但当我在Notepad++中查看输出时,所有的名称都被更改为SOH,而在Notepad中它们成为非打印字符。
2它太激进了,所以它更改了多个项目的每个实例,并用逗号分隔。 因此,从这个输出:
1. John Smith1, John Smith2, John Smith3, John Smith4<br>
This is the reason why John Smith1, John Smith2, John Smith3, and John Smith4 did what they did.<br>

在Notepad++中类似于:

1. [[SOH]], [[SOH]], [[SOH]], [[SOH]]<br>
This is the reason why John Smith1, [[SOH]], [[SOH]], and John Smith4 did what they did.<br>

我会给其他的建议试试看是否有用。你知道吗

再次感谢。你知道吗

最新更新 我解决了非印刷问题。我忘了用“r”转义正则表达式中的替换字符串。现在,如果我能让regex在第一个<br>停止,我应该得到我需要的。还在搜索。。。你知道吗

另一件事:将有更多的编号行与逗号分隔的名称和描述与字符串中的换行符。所以呢

1. FirstName1 LastName1, FirstName2 LastName2, FirstName3 LastName3<br>  
Description with FirstName1 LastName1, FirstName2 LastName2, FirstName3 LastName3<br>

2. FirstName3 LastName3, FirstName4 LastName4<br>  
Description with FirstName3 and FirstName4 LastName4.<br>

3. FirstName3 LastName3, FirstName6 LastName6<br>  
Description with FirstName3 and FirstName6.<br>

仍然只想更改以数字/句点/空格开头,以换行符结尾的行。你知道吗

1. [[FirstName1 LastName1]], [[FirstName2 LastName2]], [[FirstName3 LastName3]]<br>  
Description with FirstName1 LastName1, FirstName2 LastName2, FirstName3 LastName3<br>  

2. [[FirstName3 LastName3]], [[FirstName4 LastName4]]<br>  
Description with FirstName3 and FirstName4 LastName4.<br>  

3. [[FirstName3 LastName3]], [[FirstName6 LastName6]]<br>  
Description with FirstName3 and FirstName6.<br>

与“描述”不匹配。这只是一个例子。你知道吗


Tags: and字符串br名称withetcdescriptionjohn
3条回答

也许,一些类似的表达

(?<=\.\s|,\s)([^,\r\n]+)\s*(?=<br>|,)

以及替换

[[\1]]

可能也是一种选择。你知道吗

测试

import re

regex = r"(?<=\.\s|,\s)([^,\r\n]+)\s*(?=<br>|,)"
test_str = ("1. John Smith1, John Smith2, John Smith3, etc.<br>\n"
    "12. John Smith1, John Smith2, John Smith3, etc.<br>")
subst = "[[\\1]]"

print(re.sub(regex, subst, test_str))

输出

1. [[John Smith1]], [[John Smith2]], [[John Smith3]], [[etc.]]<br>
12. [[John Smith1]], [[John Smith2]], [[John Smith3]], [[etc.]]<br>

如果您希望简化/修改/探索表达式,在regex101.com的右上面板中已经对其进行了解释。如果您愿意,还可以在this link中查看它如何与一些示例输入匹配。你知道吗


你可以这样做

import re

st = "1. John Smith1, John Smith2, John Smith3, etc.<br>"

re.findall(r"(?:\d\. )?(.*?)(?:, |<br>)", st)

像往常一样,有两种方法可以做到这一点,但是仅仅用regex替换可能是不够的。我有两个选择:

正则表达式+字符串操作

在原始正则表达式的基础上进行扩展,可以使用此正则表达式更好地捕获并跳过第一个数字/点/空格组:

import re
st = '1. John Smith1, John Smith2, John Smith3, etc.<br>'
re1 = r"(\d\.\s)*(.+?)(?:, |(<br>)$)"
new_st = re.sub(re1, r"\1[[\2]], \3", st)
print(new_st)

这给了我们一个价值:

new_st = '1. [[John Smith1]], [[John Smith2]], [[John Smith3]], [[etc.]], <br>'

注意结尾的最后一个逗号。我们可以用以下方法移除这个:

new_st = ''.join(new_st.rsplit(", ", 1))

这给了我们:

'1. [[John Smith1]], [[John Smith2]], [[John Smith3]], [[etc.]]<br>'

所以总的来说你应该:

import re
st = '1. John Smith1, John Smith2, John Smith3, etc.<br>'
re1 = r"(\d\.\s)*(.+?)(?:, |(<br>)$)"
new_st = re.sub(re1, r"\1[[\2]], \3", st)  # notice I do capture the first group
new_st = ''.join(new_st.rsplit(", ", 1))

提取核心,然后使用split/join

这也使用正则表达式,但只提取字符串的核心。然后使用连接/拆分的组合来实现所需的结果:

import re
st = '1. John Smith1, John Smith2, John Smith3, etc.<br>'
re2 = r"(\d+\.\s+)(.+)(<br>)$"
sections = re.findall(re3, st)

# just to make it clearer i'll split the sections
the_number, the_core, the_end = sections[0]

# rework the core
the_core = ']], [['.join(the_core.split(','))

# glue all the pieces together adding what's missing
new_st = the_number + '[[' + the_core + ']]' + the_end

结果是:

'1. [[John Smith1]], [[ John Smith2]], [[ John Smith3]], [[ etc.]]<br>'

相关问题 更多 >