Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Gsub return first match

Tags:

regex

r

I want to extract the 12 and the 0 from the test vector. Every time I try it would either give me 120 or 12:0

TestVector <- c("12:0")
gsub("\\b[:numeric:]*",replacement = "\\1", x = TestVector, fixed = F)

What can I use to extract the 12 and the 0. Can we just have one where I just extract the 12 so I can change it to extract the 0. Can we do this exclusively with gsub?

like image 285
Kevin Avatar asked Mar 15 '26 17:03

Kevin


2 Answers

One option, which doesn't involve using explicit regular expressions, would be to use strsplit() and split the timestamp on the colon:

TestVector <- c("12:0")
parts <- unlist(strsplit(TestVector, ":")))
> parts[1]
[1] "12"
> parts[2]
[1] "0"
like image 62
Tim Biegeleisen Avatar answered Mar 18 '26 05:03

Tim Biegeleisen


Try this

gsub("\\b(\\d+):(\\d+)\\b",replacement = "\\1 \\2", x = TestVector, fixed = F)

Regex Breakdown

\\b #Word boundary
  (\\d+) #Find all digits before :
   :  #Match literally colon
  (\\d+) #Find all digits after :
\\b #Word boundary

I think there is no named class as [:numeric:] in R till I know, but it has named class [[:digit:]]. You can use it as

gsub("\\b([[:digit:]]+):([[:digit:]]+)\\b",replacement = "\\1  \\2", x = TestVector)

As suggested by rawr, a much simpler and intuitive way to do it would be to just simply replace : with space

gsub(":",replacement = " ", x = TestVector, fixed = F)
like image 35
rock321987 Avatar answered Mar 18 '26 06:03

rock321987



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!