regex - 基于Regex python的拆分
问题描述
解决方案
一种选择是使用re.findall
以下模式:
‘‘(.*?)’’ (.*?)(?= ‘‘|$)
这将在单独的组中为输入中找到的每个匹配项捕获公司名称和描述。请注意,我们使用前瞻(?= ‘‘|$)
作为当前描述的结尾,它要么发生在下一个条目的开头,要么发生在输入的结尾。
inp = "‘‘Apple’’ It is create by Steve Jobs (He was fired and get hired) ‘‘Microsoft’’ Bill Gates was the richest man in the world ‘‘Oracle’’ It is a database company"
matches = re.findall('‘‘(.*?)’’ (.*?)(?= ‘‘|$)', inp)
companyList = [row[0] for row in matches]
descriptionList = [row[1] for row in matches]
print(companyList)
print(descriptionList)
这打印:
['Apple', 'Microsoft', 'Oracle']
['It is create by Steve Jobs (He was fired and get hired)',
'Bill Gates was the richest man in the world', 'It is a database company']
推荐阅读
- parse-server - Parse 服务器可以处理的最大请求数
- wordpress - Bootstrap 导航栏在 Wordpress 主题中不起作用
- linux - 内核控制路径和内核线程之间有什么关系?
- python - 尝试发送邮件时 Mailgun 内部服务器错误 500
- laravel - 当 npm i imagemin 说自己没有安装?WARN img-loader@3.0.0 需要 imagemin@^5.0.0 的对等点,但没有安装
- java - 当我在 weblogic 中部署时,努力让 JPA 与 Spring 一起工作
- python - python 两个耦合二阶 ODE Runge Kutta 四阶
- javascript - $.getScript 带有用户名和密码
- php - 在 Woocommerce 3 中的客户电子邮件通知上设置跟踪号链接
- r - 需要在 R 中编写一个新的随机生成器