简体   繁体   English

Ruby正则表达式搜索

[英]Ruby regular expression search

I have such a string: 我有这样的字符串:

select count(*) from infos where pk1 = 'Information' and (pk3 = 'USA' or pk3 = 'Afrika') and value_string = 'book';

It is an SQL-statement and I have to find values for pk1, pk3 and value_string from the given statement as they can vary from statement to statement. 这是一个SQL语句,我必须从给定的语句中查找pk1,pk3和value_string的值,因为它们在语句之间可能会有所不同。 (in this example I want to find the value for pk1, which is 'Information', for pk3s, which are 'Afrika' and 'USA') (在此示例中,我想找到pk1的值,即“信息”,对于pk3的值,即“非洲”和“美国”)

My idea was to use regex such as (pk1|pk3|value_string)((\\W)*|(\\d)*)(\\w)* . 我的想法是使用正则表达式,例如(pk1|pk3|value_string)((\\W)*|(\\d)*)(\\w)*

As a statement will be entered by user, it can be of different forms as "...pk1='Information' " or "...pk1= 'Information'" and so on. 由于用户将输入一条语句,因此可以采用“ ... pk1 ='Information'”或“ ... pk1 ='Information'“等不同形式。

I am not quite satisfied with my regex and it would be very helpful to hear another suggestions. 我对我的正则表达式不太满意,听到另一个建议会很有帮助。

Thanks in advance 提前致谢

PS: sqlite3 sql PS:sqlite3 SQL

(?:pk(?:1|3)|value_string) = '([^']+)'

演示

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

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