-1

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!

  • Looks like `\d{2}:\d{2}`. There is a link http://stackoverflow.com/questions/4736/learning-regular-expressions someone is marking duplicate whenever someone asks a regex question without showing effort. –  Jun 09 '15 at 01:25
  • probably not a duplicate but generally regex questions seem to be "suggest me a [regex] tool" with no effort shown – rawr Jun 09 '15 at 01:43

3 Answers3

2

Try:

regmatches(string, gregexpr('\\d\\d:\\d\\d', string))
[[1]]
[1] "03:30" "12:20" "12:30" "15:00" "15:30" "18:00"

Notice that the colon is always surrounded by four digits. We repeat that pattern with the special regex character \\d which 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 \\d to 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,

regmatches(string, gregexpr('\\d{2}:\\d{2}', string))
[[1]]
[1] "03:30" "12:20" "12:30" "15:00" "15:30" "18:00"
Pierre L
  • 28,203
  • 6
  • 47
  • 69
2

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"
hwnd
  • 69,796
  • 4
  • 95
  • 132
1
regmatches(input,gregexpr('\\d{2}:\\d{2}',input))

OR

strsplit(gsub("(\\d{2})(?=\\d{2})","\\1 ,\\2",input,perl=T),',|-')
Shenglin Chen
  • 4,504
  • 11
  • 11