x <- '<a href="index.php">Home</a>'
y <- regexpr('>[A-Z0-9 ]{1,50}<', x, ignore.case=TRUE)
y

z <- y + attr(y, "match.length")-1
substr(x, y, z)

substr(x, y+1, z-1)


x  <- '<a href="code/120221.R" alt="regexpr">Download Code</a>'
y1 <- regexpr(">[A-Z0-9 ]{1,50}<", x, TRUE)
z1 <- y1 + attr(y1, "match.length")-1
substr(x, y1, z1)
substr(x, y1+1, z1-1)

y2 <- regexpr('href="[A-Z0-9/._ -]{1,50}"', x, TRUE)
z2 <- y2 + attr(y2, "match.length")-1
substr(x, y2, z2)
substr(x, y2+6, z2-1)

y3 <- regexpr('alt="[A-Z0-9/._ -]{1,50}"', x, TRUE)
z3 <- y3 + attr(y3, "match.length")-1
substr(x, y3, z3)
substr(x, y3+5, z3-1)


x <- c("ABCDE", "CDEFG", "FGHIJ")
regexpr("D", x)

gregexpr("D", x)

## What is the best way to select all the text between 2 tags?
## - ex: the text between all the 'pre' tags on the page.
html.text <- "<pre>This is a sentence.</pre> Another text. <br> <pre>Second sentence.</pre>"
# Split
str_extract_all(html.text, regex("<pre>(.*?)</pre>", ignore.case=T))[[1]]
grep("<pre>(.*?)</pre>", text, value=T, ignore.case = TRUE)
grep("<pre>(.|\n)*?</pre>", text, value=T, ignore.case = TRUE)

my.function <- function(x){
  n <- nchar(x)
  substring(x, 6, n-6)
}
my.function(str_extract_all(html.text, regex("<pre>(.*?)</pre>", ignore.case=T))[[1]])

"<TAG\b[^>]*>(.*?)</TAG>" matches the opening and closing pair of a specific HTML tag. 
Anything between the tags is captured into the first backreference. 
The question mark in the regex makes the star lazy, 
to make sure it stops before the first closing tag rather than before the last, 
like a greedy star would do. 
This regex will not properly match tags nested inside themselves, 
like in "<TAG>one<TAG>two</TAG>one</TAG>".

<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1> will match the opening and closing pair of any HTML tag. Be sure to turn off case sensitivity. The key in this solution is the use of the backreference \1 in the regex. Anything between the tags is captured into the second backreference. This solution will also not match tags nested in themselves.

## Find countries with letter i or t, and ends with land, 
## and replace land with LAND (using backreference).

tt <- grep("(i|t).*land$", countries, value=T, ignore.case = T)
str_replace_all(tt, "land$", "LAND")

## [1] "FinLAND"     "IceLAND"     "IreLAND"     "SwaziLAND"   "SwitzerLAND"
## [6] "ThaiLAND"

## Open a URL connection for reading
con <- url("http://www.jhsph.edu", "r")  

## Read the web page
x <- readLines(con)                      

## Print out the first few lines
head(x)                                  
[1] "<!DOCTYPE html>"                                               
[2] "<html lang=\"en\">"                                            
[3] ""                                                              
[4] "<head>"                                                        
[5] "<meta charset=\"utf-8\" />"                                    
[6] "<title>Johns Hopkins Bloomberg School of Public Health</title>"

string <- 'Este es mi texto donde \verb"tenemos" una palabra en \verb"verbatim".'
gregexpr('\verb\\"(.*?)\\"', string)
result <- gsub('\verb\\"(.*?)\\"', '<font color="green">`\\1`</font>', x = string)
writeLines(result)


