You need to enable JavaScript to run this app. Python使用正则表达式从长文本中提取指定后缀的公司名称
优惠活动
大模型
产品
解决方案
定价
更多

Python使用正则表达式从长文本中提取指定后缀的公司名称

正则调整方案

直接使用如下正则即可提取到符合要求的公司名:

import re

Companies = 'Apples Inc, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, Bananas LLC, Carrots Corp, xxxx.'
# 注意后缀按从长到短排列,避免短后缀优先匹配导致截断
pattern = re.compile(r'(?:^|, )([^,]+? (?:Inc|LLC|Corporation|Corp))', flags=re.I)
res = pattern.findall(Companies)
print(res)

运行输出:

['Apples Inc', 'Bananas LLC', 'Carrots Corp']

正则逻辑说明

  • (?:^|, ):非捕获组,匹配公司名前置的两种合法场景:字符串开头、, 分隔符,该部分不会进入最终提取结果
  • [^,]+?:非贪婪匹配所有非逗号字符,严格限定匹配范围在两个逗号之间,不会跨段吃到无关内容
  • (?:Inc|LLC|Corporation|Corp):非捕获组匹配四类后缀,按从长到短排序避免Corp提前匹配截断Corporation
  • 最外层的小括号为捕获组,findall会直接返回所有捕获组内容,不需要额外清洗

你提到的带逗号的公司名预处理方案可以和该正则兼容,预处理删除Inc前的逗号后,正则匹配逻辑不受影响。


内容的提问来源于stack exchange,提问作者Skittlebrau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 21:15:03