问题
I use http://www.regexper.com to view a picto representation regular expressions a lot. I would like a way to ideally:
- send a regular expression to the site
- 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:
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}
回答1:
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
来源:https://stackoverflow.com/questions/27489760/send-expression-to-website-return-dynamic-result-picture