我有各种文本字符串,其中包含用户名、公司名称和电话号码,它们都类似于以下内容:
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\``
}
如何找到左括号“(”和正斜杠“/”之间的文本,然后检索公司名称?
最佳答案
这可能不是一个完美的解决方案,但它工作得很好:)
s1='David Smith (Best Pool and Spa Supplies / 07438473784)'
sp1=s1.split('(')
sp2=sp1[1].split('/')
print(sp2)
输出:['最佳泳池和水疗用品', '07438473784)']
关于Python - 在其他两个特定字符之间的字符串中提取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073838/