首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本挖掘与词提取

文本挖掘与词提取
EN

Stack Overflow用户
提问于 2018-04-11 18:57:12
回答 1查看 146关注 0票数 0

我试图从R中的SQL语句中提取表名--例如,我将SQL查询导入到R中,一行将包含:

代码语言:javascript
复制
SELECT A , B
FROM Table.1 p
JOIN Table.2 pv
ON p.ProdID.1 = ProdID.1
JOIN Table.3 v
ON pv.BusID.1 = v.BusID
WHERE SubID = 15
ORDER BY v.Name;

在R中,我一直试图对SQL语句使用str拆分,该语句将每个单词拆分成一个列,创建一个数据框架,然后找到与单词"from“的匹配,并提取下一个单词,即表1。

我在如何从多个连接中提取其他表方面遇到了困难,或者如果有一种更有效的方法或包,在我的研究中我还没有遇到过。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-11 19:16:48

这里有一种使用正则表达式的方法:

代码语言:javascript
复制
lines <- strsplit("SELECT A, B
                   FROM Table.1 p
                   JOIN Table.2 pv
                   ON p.ProdID.1 = ProdID.1
                   JOIN Table.3 v
                   ON pv.BusID.1 = v.BusID
                   WHERE SubID = 15
                   ORDER BY v.Name;", split = "\\n")[[1]]

sub(".*(FROM|JOIN) ([^ ]+).*", "\\2", lines[grep("(FROM|JOIN)", lines)]) # "Table.1" "Table.2" "Table.3"

分解:

代码语言:javascript
复制
# Use grep to find the indeces of any line containing 'FROM' or 'JOIN'
keywords_regex <- "(FROM|JOIN)"
line_indeces <- grep(keywords_regex, lines) # gives: 2 3 5
table_lines <- lines[line_indeces] # get just the lines that have table names

# Build regular expression to capture the next word after either keyword
table_name_regex <- paste0(".*", keywords_regex, " ([^ ]+).*")

# The "\\2" means to replace each match with the contents of the second capture 
# group, where a capture group is defined by parentheses in the regex
sub(table_name_regex, "\\2", table_lines)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49782589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档