regex - Extracting Text in R from checkbox sort of format -


i have text following:

x<-"annual turnover in crores   ( )15-25lacs ( )25-50 lacs ( )50-75 lacs ( )75lacs 1 cr ( x ) 1-10 cr ( )10-25cr ( )25-50cr ( )above 50 crs" 

now can see there's check before 1-10 cr box. how can r extract value has box checked?

i using

a<-sub("..\(x).",x)

you can achieve want through simple pattern in regex:

regmatches(x,regexpr("(?<=\\( x \\))[^\\(]+",x,perl=true)) #[1] " 1-10 cr " 

here how pattern build.

  • the (?<=something) tells part of string after something. in case, want after ( x ). since () symbols special characters in regex, had escape them thorugh \\.

  • the [^\\(]+ part tells characters not (. because next "checkbox" value starts (. again, had escape symbol.

  • the perl=true argument needed, otherwise behind defined @ start of pattern won't valid.

hope clarifies little.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -