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.

+3


source to share


3 answers


k, a slightly better answer to the question on google,

 grep("^.+(\\.\\.t\\.)$", myStrings)

      



it works because we need to escape this dot like \\.

in R.

+2


source


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\\.\\.

+5


source


Period ( .

) matches only one character .. to remove the value of point u, use the double slash in front of the char ( \\

) point .

try this instead.

grep('^.+(\\.\\.t\\.)$', myStrings)

      

Satheesh appu

0


source







All Articles