Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression in R for word of variable length between two characters

Tags:

regex

r

gsub

How do I extract the word, wordofvariablelength from the string below.

<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">

I was able to get the first part of the string using the below code, but is there a regular expression I can use to get only the word immediately after "browse/" and before "\", which here is the word, "wordofvariablelength" using the code below

mystring = substr(mystring,nchar("<a href=\"http://www.thesaurus.com/browse/")+1,nchar("<a href=\"http://www.thesaurus.com/browse/")+20)

Note that the word, wordofvariablelength could be of any length, and so I cannot hardcode and start and end

like image 316
tubby Avatar asked Feb 11 '23 11:02

tubby


1 Answers

Through regmatches function.

> x <- "<a href=\"http://www.adrive.com/browse/wordofvariablelength\" class=\"next-button\" id=\"explore-gutter\" data-linkid=\"huiazc\"> <strong class=\"text gutter-text \">"
> regmatches(x, regexpr('.*?"[^"]*/\\K[^/"]*(?=")', x, perl=TRUE))
[1] "wordofvariablelength"

OR

> regmatches(x, regexpr('[^/"]*(?="\\s+class=")', x, perl=TRUE))
[1] "wordofvariablelength"

OR

Much more simpler one using gsub.

> gsub('.*/|".*', "", x)
[1] "wordofvariablelength"
like image 178
Avinash Raj Avatar answered Feb 13 '23 04:02

Avinash Raj