简体   繁体   English

在逻辑运算符表达式之间添加双引号

[英]Add double quotes between a logical operator expression

I have a string For example OR (Degradation parameter) I need to add double quotes between the logical expression followed by AND/OR/... in the parenthesis and add the code in python.我有一个字符串 例如OR (Degradation parameter)我需要在括号中后跟 AND/OR/... 的逻辑表达式之间添加双引号,并在 python 中添加代码。

I tried this in Regex formula (^\S*)(?:|AND|OR|\(|\))?(.*?)(?:AND|OR|\)?$)(?<![AND|OR]) , but still I can't include the double quotes inside the parenthesis.我在正则表达式公式(^\S*)(?:|AND|OR|\(|\))?(.*?)(?:AND|OR|\)?$)(?<![AND|OR]) ,但我仍然不能在括号内包含双引号。

As the output I got is "OR (Degradation parameter)" .由于我得到的 output 是"OR (Degradation parameter)" Can someone help me with it plz I am not familiar with regex101... I need the double quotes to be between the parenthesis like this OR ("Degradation parameter") .有人可以帮我吗?我不熟悉 regex101 ...我需要像这样的括号之间的双引号OR ("Degradation parameter") Thanks!谢谢!

You could use this:你可以使用这个:

import re

pattern = re.compile("((?:AND|OR)[ ]+\(.*?\))", re.IGNORECASE)

re.sub(pattern, "\"\\1\"", "OR (word1 word2)")
re.sub(pattern, "\"\\1\"", "AND (word1 word2)")

OUTPUT OUTPUT

'"AND (word1 word2)"'
'"OR (word1 word2)"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM