Home > Enterprise >  Retrieve All Characters in 2nd set of Quotations in a String
Retrieve All Characters in 2nd set of Quotations in a String

Time:10-04

I need to remove the values enclosed in the 2nd set of quotations of each of these strings (occurring after value:). I assume some regex might be the best way to go but not adept with the syntax.

# Here is my data:

Text                             
Added "Clock.Text", value: "45:45" id: 21
Added "Clock.Text", value: "90:00" id: 25
Added "Clock.Text", value: "5:58" id: 29
Added "Clock.Text", value: "11:13" id: 32
Added "Clock.Text", value: "3:04" id: 40


# Desired Output

Text                                        Time
Added "Clock.Text", value: "45:45" id: 21   45:45
Added "Clock.Text", value: "90:00" id: 25   90:00
Added "Clock.Text", value: "5:58" id: 29    5:58
Added "Clock.Text", value: "11:13" id: 32   11:13
Added "Clock.Text", value: "3:04" id: 40    3:04

CodePudding user response:

Assuming the value you are interested in comes between third and fourth " I suggest something like this:

vv <- 'Added "Clock.Text", value: "45:45" id: 21'
substr(
 vv, 
 gregexpr(pattern = '"', text = vv)[[1]][3]   1, 
 gregexpr(pattern = '"', text = vv)[[1]][4] - 1
)

CodePudding user response:

assuming that above is some headless text connection

regmatches('Added "Clock.Text", value: "45:45"', regexpr('^([^"] "){3}\\K[^"] ', 'Added "Clock.Text", value: "45:45"', perl = TRUE))
[1] "45:45" 

so far so good, now what?

strptime(regmatches('Added "Clock.Text", value: "45:45"', regexpr('^([^"] "){3}\\K[^"] ', 'Added "Clock.Text", value: "45:45"', perl = TRUE)), format='%M:%S')
[1] "2022-10-03 00:45:45 EDT"

doesn't seem like what you want, judging by above. But what class do you think your $Time could be?

  • Related