Pregunta

I'm trying to detect if a string contains a specific pattern using str_detect. The pattern I have is a series of "...." - exact number of dots is unknown. I'm trying to use str_detect as below....

However, in this particular case, str_detect returns TRUE. Wondering where I am doing it wrong and if str_detect is the right function to use at all? Hoping someone here can help?

library(stringr)
dot_pat="\\.........................";
str="The primary.objective is of the study."
str_detect(str,dot_pat)

This returns TRUE. I'm expecting FALSE since the dots in str do not follow the pattern.

Thanks in advance, simak

¿Fue útil?

Solución

Your pattern means: a dot (\\.) followed by 24 symbols. So this matches: ".objective is of the stu".

If you want to detect, say 10 dot symbols, use a pattern like this: dot_pat="\.{10}"

str_detect("The primary.objective is of the study.", "\\.{10}")
str_detect("hello..........world", "\\.{10}")

Otros consejos

Another much poorer approach would be to escape every single "." which Sean indicates is regex for "any character" unless it is escaped.

paste(rep("\\.", 10), collapse = "")
## This gives
## [1] "\\.\\.\\.\\.\\.\\.\\.\\.\\.\\."


str_detect("The primary.objective is of the study.", paste(rep("\\.", 10), collapse = ""))
str_detect("hello..........world", paste(rep("\\.", 10), collapse = ""))

Your pattern will match a stop (.) followed by 24 of ANY character as the "." means ANY character in regular expressions refer to the help for regex by typing

?regex

you can detect any number of stops from say 1 to 24 by setting your pattern to something like

dot_pat <- "\\.{1,24}"

Putting the \\ in front of the "." will make it match a stop specifically and not just ANY character.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top