Grep at the beginning of a line with a fixed value = T in R?

Like grep with fixed=T , but only at the beginning of the line?

 grep("a.", c("ab", "cac", "sss", "ca.f"), fixed = T) # 1 4 

I would like to get only the first entry. [Edit: the line that matches is unknown in advance and can be any. "A." just for example]

Thanks.

[Edit: I somehow decided this now, but any other ideas are welcome. I will take as an answer any alternative solution.

 s <- "a." res <- grep(s, c("ab", "cac", "sss", "ca.f"), fixed = T, value = T) res[substring(res, 1, nchar(s)) == s] 

]

+4
source share
5 answers

If you want to match the exact line (line 1) at the beginning of the line (line 2), then just substitute line 2 to the same length as line 1, and use ==, it should be pretty fast.

+9
source

Actually, Greg and you already mentioned the cleanest solution. I would generally dump grep altogether:

 > name <- "a#" > string <- c("a#b", "cac", "sss", "ca#f") > string[substring(string, 1, nchar(name)) == name] [1] "a#b" 

But if you really insist on grep, you can use the Dwins approach or follow the mindboggling solution:

 specialgrep <- function(x,y,...){ grep( paste("^", gsub("([].^+?|[#\\-])","\\\\\\1",x) ,sep=""), y,...) } > specialgrep(name,string,value=T) [1] "a#b" 

Perhaps I forgot to include some characters in gsub. Make sure you save the character], and the last one in the character set, otherwise you will get errors. Or just forget about it, use your own solution. This is just for fun :-)

+3
source

You want to use fixed=T because of . in the template? In this case, you can just exit . this will work:

 grep("^a\\.", c("ab", "cac", "sss", "ca.f")) 
+1
source

If you need to focus only on the first two characters, then only grep contains this information:

 > grep("a.", substr(c("ab", "cac", "sss", "ca.f"), 1,2) ,fixed=TRUE) [1] 1 

You can easily include it in a function:

 > checktwo <- function (patt,vec) { grep(patt, substr(vec, 1,nchar(patt)) ,fixed=TRUE) } > checktwo("a.", c("ab", "cac", "sss", "ca.f") ) [1] 1 
+1
source

I think Dr. G had the key to the solution in his answer, but he didn’t explicitly name it: β€œ^” in the pattern indicates β€œat the beginning of the line”. ("$" means at the end of the line)

So its "^ a". the pattern means "at the beginning of the line, find" a "followed by one character of something (". ")".

Or you could just use β€œ^ a” as a pattern if you don't want to match a single character string containing only β€œa”.

Does it help?

Jeffrey

+1
source

Source: https://habr.com/ru/post/1332593/


All Articles