GithubHelp home page GithubHelp logo

rematch-1's Introduction

rematch

Match Regular Expressions with a Nicer 'API'

Linux Build Status Windows Build status CRAN RStudio mirror downloads Coverage Status

A small wrapper on 'regexpr' to extract the matches and captured groups from the match of a regular expression to a character vector.

Installation

source("https://install-github.me/MangoTheCat/rematch")

Usage

library(rematch)
dates <- c("2016-04-20", "1977-08-08", "not a date", "2016",
  "76-03-02", "2012-06-30", "2015-01-21 19:58")
isodate <- "([0-9]{4})-([0-1][0-9])-([0-3][0-9])"
re_match(text = dates, pattern = isodate)
#>      .match                       
#> [1,] "2016-04-20" "2016" "04" "20"
#> [2,] "1977-08-08" "1977" "08" "08"
#> [3,] NA           NA     NA   NA  
#> [4,] NA           NA     NA   NA  
#> [5,] NA           NA     NA   NA  
#> [6,] "2012-06-30" "2012" "06" "30"
#> [7,] "2015-01-21" "2015" "01" "21"
isodaten <- "(?<year>[0-9]{4})-(?<month>[0-1][0-9])-(?<day>[0-3][0-9])"
re_match(text = dates, pattern = isodaten)
#>      .match       year   month day 
#> [1,] "2016-04-20" "2016" "04"  "20"
#> [2,] "1977-08-08" "1977" "08"  "08"
#> [3,] NA           NA     NA    NA  
#> [4,] NA           NA     NA    NA  
#> [5,] NA           NA     NA    NA  
#> [6,] "2012-06-30" "2012" "06"  "30"
#> [7,] "2015-01-21" "2015" "01"  "21"
github_repos <- c("metacran/crandb", "jeroenooms/[email protected]",
                  "jimhester/covr#47", "hadley/dplyr@*release",
                  "mangothecat/remotes@550a3c7d3f9e1493a2ba",
                  "/$&@R64&3")
owner_rx <- "(?:(?<owner>[^/]+)/)?"
repo_rx <- "(?<repo>[^/@#]+)"
subdir_rx <- "(?:/(?<subdir>[^@#]*[^@#/]))?"
ref_rx <- "(?:@(?<ref>[^*].*))"
pull_rx <- "(?:#(?<pull>[0-9]+))"
release_rx <- "(?:@(?<release>[*]release))"
ref_or_pull_or_release_rx <-
  sprintf("(?:%s|%s|%s)?", ref_rx, pull_rx, release_rx)
github_rx <- sprintf("^(?:%s%s%s%s|(?<catchall>.*))$",
                     owner_rx, repo_rx, subdir_rx, ref_or_pull_or_release_rx)
out <- re_match(text = github_repos, pattern = github_rx)
out
#>      .match                                     owner         repo     
#> [1,] "metacran/crandb"                          "metacran"    "crandb" 
#> [2,] "jeroenooms/[email protected]"                   "jeroenooms"  "curl"   
#> [3,] "jimhester/covr#47"                        "jimhester"   "covr"   
#> [4,] "hadley/dplyr@*release"                    "hadley"      "dplyr"  
#> [5,] "mangothecat/remotes@550a3c7d3f9e1493a2ba" "mangothecat" "remotes"
#> [6,] "/$&@R64&3"                                ""            ""       
#>      subdir ref                    pull release    catchall   
#> [1,] ""     ""                     ""   ""         ""         
#> [2,] ""     "v0.9.3"               ""   ""         ""         
#> [3,] ""     ""                     "47" ""         ""         
#> [4,] ""     ""                     ""   "*release" ""         
#> [5,] ""     "550a3c7d3f9e1493a2ba" ""   ""         ""         
#> [6,] ""     ""                     ""   ""         "/$&@R64&3"

License

MIT © Mango Solutions

rematch-1's People

Contributors

gaborcsardi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.