0

I have cells with text with numbers in brackets such as (1). I was given help with stripping out text and returning the number in brackets.

newdataframe<- data.frame(gsub("[^()0-9]","",as.matrix(UltraCodes)))

However, my numbers in brackets go up to 13 and I didn't realise that the above code doesn't cover that. I think the above relates to regex and I've looked at answers on how to return double digits

Why doesn't [01-12] range work as expected?

So I've various ways to return double digits which work. Except I can't return 10 which still get returned as 1.

newdataframe<- data.frame(gsub("[^()^([1-9]|[0-1][0-2])$","",as.matrix(UltraCodes)))

newdataframe<- data.frame(gsub("[^()[1-9]|1[0-3]]","",as.matrix(UltraCodes)))

newdataframe<- data.frame(gsub("[^()[1-9]|1[1-9][0-9]]","",as.matrix(UltraCodes)))

How do I get 10 to be selected?

Lan
  • 25
  • 2

1 Answers1

1

To capture up to 2 digits number enclosed in brackets use this regular expression

(\([\d]{1,2}\))

For capturing as many as possible digits number enclosed by brackets, use this

(\([\d]+\))
Akay Nirala
  • 1,136
  • 7
  • 13