开发者

Python regex: find lines where period is missing in

I'm looking for a regular express开发者_StackOverflowion, implemented in Python, that will match on this text

WHERE PolicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753' 

but will not match on this text

WHERE AsPolicy.PolicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753' 

I'm doing this to find places in a large piece of SQL where the developer did not explicitly reference the table name. All I want to do is print the offending lines (the first WHERE clause above). I have all of the code done except for the regex.


re.compile('''WHERE [^.]+ =''')

Here, the [] indicates "match a set of characters," the ^ means "not" and the dot is a literal period. The + means "one or more."

Was that what you were looking for?


something like

WHERE .*\..* = .*

not sure how accurate can be, it depends on how your data looks... If you provide a bigger sample it can be refined


Something like this would work in java, c#, javascript, I suppose you can adapt it to python:

/WHERE +[^\.]+ *\=/


>>> l
["WHERE PolicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753' ", "WHERE AsPolicy.P
olicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753' "]
>>> [line for line in l if re.match('WHERE [^.]+ =', line)]
["WHERE PolicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753' "]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜