19

Lets say I have a string "Hello." I want to see if this string contains a period:

text <- "Hello."
results <- grepl(".", text)

This returns results as TRUE, but it would return that as well if text is "Hello" without the period.

I'm confused, I can't find anything about this in the documentation and it only does this for the period.

Any ideas?

oguz ismail
  • 1
  • 16
  • 47
  • 69
marc
  • 2,037
  • 9
  • 24
  • 32
  • 10
    `"."` is *any* character. ***Anything***. You meant `"\\."`. – Simon O'Hanlon Oct 24 '13 at 15:45
  • 5
    Au contraire! The documentation spends an enormous amount of time describing how `grepl` uses regular expressions, in which `.` is a special character. There are even link in the documentation to the sections on regular expressions, and much discussion of how setting `fixed = TRUE` will perform exact matching, not using regular expressions. – joran Oct 24 '13 at 15:46
  • Spot on. @joran, I'll need to comb through it, because I don't see it in the Pattern Matchin and Replacement text. – marc Oct 24 '13 at 15:50
  • @marc under the very first argument `pattern` it says `character string containing a regular expression` where *regular expression* is a link to the page on regular expressions where this information is sensibly contained! :-) – Simon O'Hanlon Oct 24 '13 at 15:56
  • @SimonO101: huge help! Thanks. – marc Oct 24 '13 at 15:58
  • 4
    In marc's defense, I too once found it quite confusing that `?regexpr` does not itself document regular expression matching rules, and that what one really needs is `?regex` – Josh O'Brien Oct 24 '13 at 17:13

2 Answers2

29

See the differences with these examples

 > grepl("\\.", "Hello.")
[1] TRUE
> grepl("\\.", "Hello")
[1] FALSE

the . means anything as pointed out by SimonO101, if you want to look for an explicit . then you have to skip it by using \\. which means look for a .

R documentation is extensive on regular expressions, you can also take a look at this link to understand the use of the dot.

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • Does ´\\´ affect the subsequent character (in this case, the dot), or is there a way to make it encompass a number of subsequent characters? Let's say I don't want to write ´\\.\\.\\.´ to match ´"..."´. Can I do ´\\(.)´ or something like that? – Dr. Fabian Habersack Aug 14 '19 at 11:38
  • @Fabian Habersack For you to capture one or more dots (.), you have to use the quantifier `+`, so you can write `\\.+` – Jilber Urbina Aug 14 '19 at 14:49
  • Okay, but what if I need to match either a specific amount of dots which let's say equaly 10 or what if I want to match 10+ times a dot? Can I do `\\.{10}` ? Plus, how would I match "10 or more"? – Dr. Fabian Habersack Aug 15 '19 at 10:59
  • 1
    @FabianHabersack you can read some basic regex rules [here](https://regexone.com/lesson/repeating_characters) – Jilber Urbina Aug 15 '19 at 15:25
  • Why `\\.` not conventional escape `\.` ? – jessexknight Feb 13 '20 at 17:22
  • for 3 consecuttive dots `...` we use `\\.\\.\\.` other wise it would confuse with dots interspersed with other characters. – ambrish dhaka Sep 30 '20 at 03:43
17

I use Jilber's approach usually but here are two other ways:

> grepl("[.]", "Hello.")
[1] TRUE
> grepl("[.]", "Hello")
[1] FALSE

> grepl(".", "Hello.", fixed = TRUE)
[1] TRUE
> grepl(".", "Hello", fixed = TRUE)
[1] FALSE
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519