Say, if I have such a string:
03:30-12:20, 12:30-15:0015:30-18:00
and i need to break them into an array:
03:30 12:20 12:30 15:00 15:30 18:00
Can anyone suggest what regex and R function I should use to do so? Thanks!
On
regmatches(input,gregexpr('\\d{2}:\\d{2}',input))
OR
strsplit(gsub("(\\d{2})(?=\\d{2})","\\1 ,\\2",input,perl=T),',|-')
On
I contributed a regex solely for this in the qdapRegex package.
library(qdapRegex)
x <- '03:30-12:20, 12:30-15:0015:30-18:00'
rm_time(x, extract=T)[[1]]
# [1] "03:30" "12:20" "12:30" "15:00" "15:30" "18:00"
Try:
Notice that the colon is always surrounded by four digits. We repeat that pattern with the special regex character
\\dwhich means digit.[0-9]is used in the other answer and is just as good, if not better for advanced regex tokenizing operations. I used\\dto show other avenues to the same goal.You can also specify how many digits should be matched with curly braces. In this case, 2 digits is what we're looking for around colons,