Python - 在其他两个特定字符之间的字符串中提取文本?

Posted

技术标签:

【中文标题】Python - 在其他两个特定字符之间的字符串中提取文本?【英文标题】:Python - Extracting text in a string between two other specific characters? 【发布时间】:2018-12-07 00:03:02 【问题描述】:

我有各种包含用户名、公司名称和电话号码的文本字符串,它们都类似于以下内容:

FirstName LastName (Some Business Name / phoneNumber)
FirstName LastName (Business Name / phoneNumber)
FirstName LastName (BusinessName / differentphoneNumber)
FirstName LastName (Short Name / somephoneNumber)
FirstName LastName (Very Long Business Name / otherphoneNumber)

现实世界的示例可能如下所示:

David Smith (Best Pool and Spa Supplies / 07438473784)
Bessy McCarthur Jone (Dog Supplies / 0438-343522)

我已使用此代码提取名字(正如我之前需要的那样)并且效果很好:

import re
details = re.findall(r'^[\w+]+', input_data['stripeDescription'])
return 
'firstName': details[0] if details else None\``

如何查找左括号“(”和正斜杠“/”之间的文本,然后检索公司名称?

【问题讨论】:

【参考方案1】:

这可能不是一个完美的解决方案,但效果很好:)

s1='David Smith (Best Pool and Spa Supplies / 07438473784)'
sp1=s1.split('(')
sp2=sp1[1].split('/')
print(sp2)

输出:['最佳泳池和水疗用品','07438473784)']

【讨论】:

这非常有效。我试图添加我对这段代码的看法,但它不会出现在这些 cmets 中。所以谢谢你的帮助 @robster,请记住,如果商家名称中包含(,则此操作无效。 是的,我没有考虑错误处理,我只是想给出一个如何解决主要问题的想法:) 这就是为什么评估正则表达式是否是最佳解决方案很有用:-)。虽然...可能要注意 / 或 ( 在公司名称中。您可以反转字符串以从右侧获取 / 。或使用 sp1[-1] ?【参考方案2】:

使用括号将要在您用于re.findall 的正则表达式中匹配的模式分组:

s = '''David Smith (Best Pool and Spa Supplies / 07438473784)
Bessy McCarthur Jone (Dog Supplies / 0438-343522)'''
import re
print(re.findall(r'\(([^/]+?) */', s))

这个输出:

['Best Pool and Spa Supplies', 'Dog Supplies']

【讨论】:

正则表达式让我大开眼界。我正在努力改进它,感谢您的回复。我将使用 regex101 工具研究更多内容,看看我是否能理解它。非常感谢【参考方案3】:

这是相当健壮的,但不会处理带有括号的名称。即它希望第一个 ( 超出名称的界限。但是,您可能会通过注意到该业务中包含 \).*\( 来知道有什么问题。

data = """
David Smith (Best Pool and Spa Supplies / 07438473784)
David Smith2 (Best Pool/Spa Supplies / 07438473784)
Bessy McCarthur Jone (Dog Supplies / 0438-343522)
Bessy McCarthur Jone2 (Dog (and cat) Supplies / 0438-343522)
Bessy (Bess, fails) McCarthur Jone3 (Dog Supplies / 0438-343522)
"""

lines = [line.strip() for line in data.splitlines() if line.strip()]

for line in lines:
    name,rest = line.split("(",1)
    name = name.strip()
    phone = rest.rsplit("/")[1].replace(")","").strip()
    biz = rest.rsplit("/",1)[0].strip()
    print("\n "+line)
    print(" =>name:%s: phone:%s:biz:%s:" % (name, phone,biz))

输出:

 David Smith (Best Pool and Spa Supplies / 07438473784)
 =>name:David Smith: phone:07438473784:biz:Best Pool and Spa Supplies:

 David Smith2 (Best Pool/Spa Supplies / 07438473784)
 =>name:David Smith2: phone:Spa Supplies:biz:Best Pool/Spa Supplies:

 Bessy McCarthur Jone (Dog Supplies / 0438-343522)
 =>name:Bessy McCarthur Jone: phone:0438-343522:biz:Dog Supplies:

 Bessy McCarthur Jone2 (Dog (and cat) Supplies / 0438-343522)
 =>name:Bessy McCarthur Jone2: phone:0438-343522:biz:Dog (and cat) Supplies:

 Bessy (Bess, fails) McCarthur Jone3 (Dog Supplies / 0438-343522)
 =>name:Bessy: phone:0438-343522:biz:Bess, fails) McCarthur Jone3 (Dog Supplies:

【讨论】:

以上是关于Python - 在其他两个特定字符之间的字符串中提取文本?的主要内容,如果未能解决你的问题,请参考以下文章

Python 抓取两个特定字符之间的子字符串

替换出现在两个特定单词之间的一组字符串的所有出现

在其他两个保守字符串之间提取字符串并允许 python 或 R 中的不匹配

正则表达式在 Python 中查找两个字符串之间的字符串

Python提取两个字符串之间的内容

在Python中的数据框中的每一行的两个子字符串之间选择字符串