使用模式进行部分字符串匹配 [英] Partial string matching using patterns

查看:57
本文介绍了使用模式进行部分字符串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 R 中编写一个查询来匹配列名中的部分字符串.我正在寻找类似于 SQL 中的 LIKE 运算符的东西.例如,如果我知道字符串的开头、中间或结尾部分,我会以以下格式编写查询:

I need to write a query in R to match partial string in column names. I am looking for something similar to LIKE operator in SQL. For e.g, if I know beginning, middle or end part of the string I would write the query in format:

LIKE 'beginning%middle%' 

在 SQL 中,它将返回匹配的字符串.在 pmatchgrep 中,我似乎只能指定 'beginning' 、 'end' 而不是顺序.我正在寻找的 R 中是否有类似的功能?

in SQL and it would return matching strings. In pmatch or grep it seems I can only specify 'beginning' , 'end' and not the order. Is there any similar function in R that I am looking for?

例如,假设我正在寻找向量:

For example, say I am looking in the vector:

y <- c("I am looking for a dog",
       "looking for a new dog", "a dog", "I am just looking")

假设我想编写一个查询,它选择 寻找新狗" 并且我知道字符串的开头是 "looking" 并且字符串的结尾是狗".如果我执行 grep("dog",y) 它将返回 1,2,3.有什么办法可以在 grep 中指定开始和结束?

Let's say I want to write a query which picks "looking for a new dog" and I know start of the string is "looking" and end of string is "dog". If I do a grep("dog",y) it will return 1,2,3. Is there any way I can specify beginning and end in grep?

推荐答案

grep 函数支持正则表达式,有了正则表达式,你几乎可以匹配任何东西

The grep function supports regular expressions and with regular expressions, you can match almost anything

y<- c("I am looking for a dog", "looking for a new dog", "a dog", "I am just looking")
grep("looking.*dog",y, value=T)
# [1] "I am looking for a dog" "looking for a new dog" 

这里这个模式先寻找looking,然后是maybe something",然后是dog.所以这应该做你想做的.

Here this pattern looks for looking then "maybe something" then dog. So that should do what you want.

这篇关于使用模式进行部分字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆