python - 如何仅将引号添加到感兴趣的子字符串?
问题描述
我编写了一个解析器,它能够从字符串中获取信息。我不知道如何在感兴趣的子字符串周围添加引号。让我举例说明:
我收到了这条消息:
message = 'I have two variables: -mass: 12 --vel= 18 OR this is just another descriptor AND that new thing OR that newfangled thing'
我需要在特定子字符串(遵循布尔运算符)周围添加引号,如下所示:
message = 'I have two variables: -mass: 12 --vel= 18 OR "this is just another descriptor" AND "that new thing" OR "that newfangled thing"'
我已经这样做了:
attributes = ['OR', 'AND', 'NOT']
message = 'I have two variables: -mass: 12 --vel= 18 OR this is just another descriptor AND that new thing OR that new fangled thing'
for attribute in attributes:
modified_attribute = ' '+attribute+' '
message = modified_attribute.join('"{}"'.format(s.strip()) for s in message.split(attribute))
if attributes.index(attribute)>0: message = message[1:-1]
print(message)
但是,它返回了这个,这不是我想要的:
"I have two variables: -mass: 12 --vel= 18" OR "this is just another descriptor" AND "that new thing" OR "that new fangled thing"
上面的第一句应该有引号,因为它前面没有布尔运算符。我该怎么办?
编辑:我正在寻找一种可扩展的解决方案,可用于引用字符串中任意数量的子字符串。
解决方案
您可以使用带有前瞻的正则表达式,如下所示:
import re
message = re.sub(r'(\b(?:{0})\b) (.*?)(?=\s*\b(?:{0}|$)\b)'.format('|'.join(map(re.escape, attributes))), r'\1 "\2"', message)
message
会变成:
I have two variables: -mass: 12 --vel= 18 OR "this is just another descriptor" AND "that new thing" OR "that new fangled thing"
推荐阅读
- node.js - Nodejs Elastic benastalk拒绝连接到上游/上游过早关闭
- c - 只打印第一行(文件 i/o)
- python - 为什么 Python 不能识别 Beautifulsoup
- javascript - 在 App.js 文件中为 React 应用添加 Footer 和 Navigation,包括 React 路由?
- php - 使用 aws-php-sdk 将值附加到 DynamoDB 编号集
- ios - Swift/Combine - 将过滤后的对象分配给类的属性
- python-3.x - 对于给定的两个序列,我在编写程序以组合两个序列并按字母顺序排列时遇到输入错误?
- python-3.x - 多行字符串python3之间的输入
- python-3.x - zipfile :仅压缩目录中存在的文件
- mysql - 与从 MS SQL Server 到 MySQL 的阿拉伯文本集成