R Regular expression for a line containing full stops
I have a bunch of lines, some of which end in ..t.
. I'm trying to find a regex to match these strings, but dealing with full stops is giving me a headache!
I tried
grep('^.+(..t.)$', myStrings)
but this also matches strings like w...gate
. I think I am handling full stops wrong. Any help is generally appreciated.
Note. I am using grep inside R.
source to share
Since you are checking if the end of the line ends with ..t.
, you can delete ^.+
in your template.
A period .
in regular expression syntax is a special meaning character that matches any character other than a newline sequence. To match a literal dot or any other character of special meaning, you need to exit \\
.
> x <- c('foo..t.', 'w...gate', 'bar..t.foo', 'bar..t.')
> grep('\\.{2}t\\.$', x)
# [1] 1 4
Or place this symbol inside a symbol class.
> x <- c('foo..t.', 'w...gate', 'bar..t.foo', 'bar..t.')
> grep('[.]{2}t[.]$', x)
# [1] 1 4
Note: I used the range operator \\.{2}
to match two points instead of speeding it up twice\\.\\.
source to share