Minh Phúc Huỳnh
Minh Phúc Huỳnh

Reputation: 31

Detect digits within character string of specific form in R

I have a string like this:

TransID [12345, 67890]; Btool_ID [22222]; Reconsile [ccccc]; RequestID [ aaaaa];

I want to extract the string in TransID[], but when I use the code:

gsub(".*TransID \\[(.*)\\].*", "\\1", data)

It gave me:

12345, 67890]; Btool_ID [22222]; Reconsile [ccccc]; RequestID [ aaaaa

My desired output is:

"12345, 67890"

How could I do that? Thank you

Upvotes: 0

Views: 49

Answers (1)

Sotos
Sotos

Reputation: 51592

How about,

sub('TransID \\[(\\d+, \\d+)\\].*', '\\1', x)
#[1] "12345, 67890"

Upvotes: 1

Related Questions