Send expression to the site to return a dynamic result (image)

I use http://www.regexper.com to view regular expression icons. I would ideally ideally like:

  • send regular expression to site
  • open the site with the displayed expression

For example, use the regular expression: "\\s*foo[AZ]\\d{2,3}" . I went to the site and insert \s*foo[AZ]\d{2,3} (note the removal of double slashes). And it returns:

enter image description here

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

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

 x <- "\\s*foo[AZ]\\d{2,3}" cat(x) \s*foo[AZ]\d{2,3} 
+6
source share
1 answer

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[AZ]\\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[AZ]%5cd%7b2,3%7d" 

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

After that, there are two options:

  • Open the result in a browser
  • Just return the full encoded URL
+8
source

Source: https://habr.com/ru/post/979647/


All Articles