Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send expression to website return dynamic result (picture)

Tags:

r

rcurl

I use http://www.regexper.com to view a picto representation regular expressions a lot. I would like a way to ideally:

  1. send a regular expression to the site
  2. open the site with that expression displayed

For example let's use the regex: "\\s*foo[A-Z]\\d{2,3}". I'd go tot he site and paste \s*foo[A-Z]\d{2,3} (note the removal of the double slashes). And it returns:

enter image description here

I'd like to do this process from within R. Creating a wrapper function like view_regex("\\s*foo[A-Z]\\d{2,3}") and the page (http://www.regexper.com/#%5Cs*foo%5BA-Z%5D%5Cd%7B2%2C3%7D) with the visual diagram would be opened with the default browser.

I think RCurl may be appropriate but this is new territory for me. I also see the double slash as a problem because http://www.regexper.com expects single slashes and R needs double. I can get R to return a single slash to the console using cat as follows, so this may be how to approach.

x <- "\\s*foo[A-Z]\\d{2,3}"

cat(x)
\s*foo[A-Z]\d{2,3}
like image 770
Tyler Rinker Avatar asked Dec 15 '14 17:12

Tyler Rinker


1 Answers

Try something like this:

Query <- function(searchPattern, browse = TRUE) {
  finalURL <- paste0("http://www.regexper.com/#", 
         URLencode(searchPattern))
  if (isTRUE(browse)) browseURL(finalURL)
  else finalURL
}

x <- "\\s*foo[A-Z]\\d{2,3}"
Query(x)             ## Will open in the browser
Query(x, FALSE)      ## Will return the URL expected
# [1] "http://www.regexper.com/#%5cs*foo[A-Z]%5cd%7b2,3%7d"

The above function simply pastes together the web URL prefix ("http://www.regexper.com/#") and the encoded form of the search pattern you want to query.

After that, there are two options:

  • Open the result in the browser
  • Just return the full encoded URL
like image 191
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 15 '22 01:11

A5C1D2H2I1M1N2O1R2T1