【问题标题】:string parsing/replace in python(Using reg expression)python中的字符串解析/替换(使用正则表达式)
【发布时间】:2017-02-17 15:11:51
【问题描述】:

我正在尝试编写解析器:将字符串转换为查询格式。并停留在字符串替换的特定点(通过匹配模式)。

我无法弄清楚正则表达式模式匹配。

我有一个像

这样的 input_string
ip_query_string = "CITY == 'Mumbai' & LOCATION in ['Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai']"

#Mark the """& after CITY == """ and  """ in after LOCATION""".
#Then there is another "& and a string ' in '" inside values for in-condition.

#My output should be:
op_query_string = "CITY == 'Mumbai' AND LOCATION IN ['Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai']"

#if i will find  ' & ' or a ' in ' (before and after there are spaces):: I have to replace them with ' AND ' and ' IN ' respectively.(In this case a ip_string.replace(' & ', ' AND ').replace(' in ', ' In ')) would work.BUT read next point.
#And if they are inside a in-condition values like 'Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai' then don't replace them. keep them as is.
#If you look at op_string in-condition, the & and in are not replaced.

请帮助形成一个逻辑。

或者什么是 reg 模式(如果 & 或与其他字符一起用单引号括起来,不要替换,否则替换)?

【问题讨论】:

  • 为什么'Harry's应该变成'Harrys?
  • 对不起,有一个错字,一直是哈利。编辑了问题。

标签: python regex string parsing


【解决方案1】:

使用re.sub()函数的短解:

import re

ip_query_string = "CITY == 'Mumbai' & LOCATION in ['Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai']"
op_query_string  = re.sub(r'^([^[]+?)(in)', r'\1IN', re.sub(r'^([^[]+?)(&)', r'\1AND', ip_query_string))

print(op_query_string)

输出:

CITY == 'Mumbai' AND LOCATION IN ['Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai']

【讨论】:

  • 在这种情况下有效(问题),但如果我在正常条件的值中有一个 '&' 或 'in' 则失败。就像 ip_query_string = "CITY == 'Mumbai in Goa' & LOCATION in ['Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai']"......对于 CITY 值,我在' .然后上面的 reg_expr 导致“CITY == 'Mumbai IN Goa' AND LOCATION in ['Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai']”...i:e 它正在改变 'Mumbai in果阿”到“孟买在果阿”。这不应该发生。如果 ' & ' 和 ' in ' 包含在单引号内,则不应更改。
【解决方案2】:

它以某种奇怪的方式工作(可能不是 pythonic),但它确实有效。

def rplc_str(s):
   sp = s.split("'")
   print('After split==',sp)
   sp1 = [x.replace(' & ', ' AND ') if ((x.startswith(' &')) or (x.startswith('] &'))) else x for x in sp]
   print('After replacing & ==',sp1)
   sp2 = [x.replace(' in ', ' IN ') if x.endswith(' [') else x for x in sp1]
   print('After replacing in ==',sp1)
   return "'".join(sp2)

ip_str = "CITY == 'Mumbai' & LOCATION in ['Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai']"
op_str = rplc_str(ip_str)
print(op_str)
#After split== ['CITY == ', 'Mumbai', ' & LOCATION in [', 'Harrys Bar & Cafe: Mumbai', ',', 'Hard Rock Cafe in Mumbai', ']']
#After replace & == ['CITY == ', 'Mumbai', ' AND LOCATION in [', 'Harrys Bar & Cafe: Mumbai', ',', 'Hard Rock Cafe in Mumbai', ']']
#After replace in == ['CITY == ', 'Mumbai', ' AND LOCATION in [', 'Harrys Bar & Cafe: Mumbai', ',', 'Hard Rock Cafe in Mumbai', ']']
#CITY == 'Mumbai' AND LOCATION IN ['Harrys Bar & Cafe: Mumbai','Hard Rock Cafe in Mumbai']

希望它对某人有所帮助,但仍在等待一些 Pythonic 答案(我的意思是 reg expr。)

【讨论】:

    猜你喜欢
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2015-02-27
    相关资源
    最近更新 更多