给出以下字符串:
mystring = "animals.dog > 12 and type.type_id=105 and colors.name='yellow'"
我想从此字符串返回以下元素。
table1 = animals
element1 = dog
operator1= greater
table2 = type
element2 = type_id
operator2= equal
table3 = colors
element3 = name
operator3 = equal
我只对前3个部分感兴趣,即table.element运算符(例如animals.dog>)
不幸的是,我已经无法将句子转换为列表
我目前已经尝试了以下方法,但是通过这种方法,操作员无法继续阅读。
import re
mystring = "animals.dog > 12 and type.type_id=105 and colors.name='yellow'"
wordList = re.sub("[^\w]", " ", mystring).split()
如果您能帮助我,我会很高兴。 最好的问候
答案 0 :(得分:1)
以下是适用于此示例的正则表达式:
out = re.findall("([^\W]+)\.([^\W]+)\W*([<>=])", mystring)
输出:
[('animals', 'dog', '>'), ('type', 'type_id', '='), ('colors', 'name', '=')]
但是它不支持>=
或<=
之类的运算符。如果需要它们,则需要在正则表达式中指定它们。
有了输出列表,您可以像这样遍历它:
for table, element, operator in out:
...