Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regular expressions in R strsplit

Tags:

regex

r

strsplit

I would like to split "2015-05-13T20:41:29+0000" into 2015-05 and 20:41:29+0000. I tried the following:

> strsplit("2015-05-13T20:41:29+0000",split="-\\d\\dT",fixed=TRUE)
[[1]]
[1] "2015-05-13T20:41:29+0000"

but the pattern is not matched. How to fix this?

like image 303
Leonardo Avatar asked Jul 05 '15 16:07

Leonardo


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What is difference [] and () in regex?

In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.

Does R use regex?

Two types of regular expressions are used in R, extended regular expressions (the default) and Perl-like regular expressions used by perl = TRUE . There is also fixed = TRUE which can be considered to use a literal regular expression.


1 Answers

You can remove the fixed since you are not using exact matching,

strsplit("2015-05-13T20:41:29+0000",split="-\\d{2}T")
# [[1]]
# [1] "2015-05"       "20:41:29+0000"
like image 159
Rorschach Avatar answered Sep 30 '22 16:09

Rorschach