【发布时间】:2019-02-10 19:40:04
【问题描述】:
我不知道如何称呼这个标题,如果您认为有更好的名称,请随时编辑它。
我要做的是找到符合某些搜索条件的案例。
具体来说,我试图找到其中包含单词“where”的句子。一旦我确定了这一点,我就会尝试查找单词“SQL”命令也位于同一标记内的情况。
假设我有一个如下所示的数据框:
search_criteria = ['where']
df4
Q R
0 file.sql <sentence>dave likes stuff</sentence><properties>version = "2", description = "example" type="SqlCommand">select id, name, from table where criteria = '5'</property><sentence>dave hates stuff>
0 file.sql <sentence>dave likes stuff</sentence><properties>version = "2", description = "example">select id, name, from table where criteria = '5'</properties><sentence>dave hates stuff>
我正在尝试返回这个:
Q R
0 file.sql <properties>version = "2", description = "example">select id, name, from table</properties>
应该返回这条记录,因为它同时包含“where”和“sqlcommand”。
这是我目前的流程:
regex_stuff = df_all_xml_mfiles_tgther[cc:cc+1].R.str.findall('(<[^<]*?' + 'where' + '[^>]*?>)', re.IGNORECASE)
sql_command_regex_stuff = df_all_xml_mfiles_tgther[cc:cc+1].R.str.findall('(<property[^<]*?' + 'sqlcommand' + '[^>]*?<\/property>)', re.IGNORECASE)
if not regex_stuff.empty: #if one of the search criteria is found
if not sql_command_regex_stuff.empty: #check to see if the phrase "sqlcommand" is found anywhere as well
(insert rest of code)
这不会返回任何东西。
我做错了什么?
编辑#1:
看来我需要在最后做点什么,让正则表达式看起来像这样:
<property[^<]*?SqlCommand[^(<\/property>)]*
我觉得这是正确的方向,行不通,但我觉得这是正确的一步。
【问题讨论】:
-
您正在尝试查找字面上包含单词“sqlcommand”的行。这是你的意图吗?
-
是的,另外,我澄清了我的第一篇文章
标签: python python-3.x pandas