qdapRegex v0.7.2

0

Monthly downloads

0th

Percentile

Regular Expression Removal, Extraction, and Replacement Tools

A collection of regular expression tools associated with the 'qdap' package that may be useful outside of the context of discourse analysis. Tools include removal/extraction/replacement of abbreviations, dates, dollar amounts, email addresses, hash tags, numbers, percentages, citations, person tags, phone numbers, times, and zip codes.

Readme

qdapRegex

Project Status: Active - The project has reached a stable, usable state and is being actively developed. Build Status Coverage Status DOI Version

qdapRegex is a collection of regular expression tools associated with the qdap package that may be useful outside of the context of discourse analysis. Tools include removal/extraction/replacement of abbreviations, dates, dollar amounts, email addresses, hash tags, numbers, percentages, citations, person tags, phone numbers, times, and zip codes. Functions that remove/replace are prefixed with rm_. Each of these functions has an extraction counterpart prefixed with ex_.

The qdapRegex package does not aim to compete with string manipulation packages such as stringr or stringi but is meant to provide access to canned, common regular expression patterns that can be used within qdapRegex, with R's own regular expression functions, or add on string manipulation packages such as stringr and stringi.

The functions in qdapRegex work on a dictionary system. The current implementation defaults to a United States flavor of canned regular expressions. Users may submit proposed region specific regular expression dictionaries that contain the same fields as the regex_usa data set or improvements to regular expressions in current dictionaries. Please submit proposed regional regular expression dictionaries via: https://github.com/trinker/qdapRegex/issues

Educational

The qdapRegex package serves a dual purpose of being both functional and educational. While the canned regular expressions are useful in and of themselves they also serve as a platform for understanding regular expressions in the context of meaningful, purposeful usage. In the same way I learned guitar while trying to mimic Eric Clapton, not by learning scales and theory, some folks may enjoy an approach of learning regular expressions in a more pragmatic, experiential interaction. Users are encouraged to look at the regular expressions being used (?regex_usa and ?regex_supplement are the default regular expression dictionaries used by qdapRegex) and unpack how they work. I have found slow repeated exposures to information in a purposeful context results in acquired knowledge.

The following regular expressions sites were very helpful to my own regular expression education:

  1. Regular-Expression.info
  2. Rex Egg
  3. Regular Expressions as used in R
  4. Debuggex (Visualizing Regex)

Being able to discuss and ask questions is also important to learning...in this case regular expressions. I have found the following forums extremely helpful to learning about regular expressions:

  1. Talk Stats + Posting Guidelines
  2. stackoverflow + Posting Guidelines

Installation

To download the development version of qdapRegex:

Download the zip ball or tar ball, decompress and run R CMD INSTALL on it, or use the pacman package to install the development version:

if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh("trinker/qdapRegex")

Contact

You are welcome to:

Examples

The following examples demonstrate some of the functionality of qdapRegex.

library(qdapRegex)

Extract Citations

w <- c("Hello World (V. Raptor, 1986) bye (Foo, 2012, pp. 1-2)",
    "Narcissism is not dead (Rinker, 2014)",
    "The R Core Team (2014) has many members.",
    paste("Bunn (2005) said, \"As for elegance, R is refined, tasteful, and",
        "beautiful. When I grow up, I want to marry R.\""),
    "It is wrong to blame ANY tool for our own shortcomings (Baer, 2005).",
    "Wickham's (in press) Tidy Data should be out soon.",
    "Rinker's (n.d.) dissertation not so much.",
    "I always consult xkcd comics for guidance (Foo, 2012; Bar, 2014).",
    "Uwe Ligges (2007) says, \"RAM is cheap and thinking hurts\"",
    "Silly (Bar, 2014) stuff is what Bar (2014, 2012) said."
)

ex_citation(w)
## [[1]]
## [1] "V. Raptor, 1986" "Foo, 2012"      
## 
## [[2]]
## [1] "Rinker, 2014"
## 
## [[3]]
## [1] "The R Core Team (2014)"
## 
## [[4]]
## [1] "Bunn (2005)"
## 
## [[5]]
## [1] "Baer, 2005"
## 
## [[6]]
## [1] "Wickham's (in press)"
## 
## [[7]]
## [1] "Rinker's (n.d.)"
## 
## [[8]]
## [1] "Foo, 2012" "Bar, 2014"
## 
## [[9]]
## [1] "Uwe Ligges (2007)"
## 
## [[10]]
## [1] "Bar, 2014"        "Bar (2014, 2012)"
as_count(ex_citation(w))
##             Author     Year n
## 7              Bar     2014 3
## 6              Foo     2012 2
## 2             Baer     2005 1
## 5              Bar     2012 1
## 3             Bunn     2005 1
## 8           Rinker     2014 1
## 11          Rinker     n.d. 1
## 9  The R Core Team     2014 1
## 4       Uwe Ligges     2007 1
## 1        V. Raptor     1986 1
## 10         Wickham in press 1

Extract Twitter Hash Tags, Name Tags, & URLs

x <- c("@hadley I like #rstats for #ggplot2 work.",
    "Difference between #magrittr and #pipeR, both implement pipeline operators for #rstats:
        http://renkun.me/r/2014/07/26/difference-between-magrittr-and-pipeR.html @timelyportfolio",
    "Slides from great talk: @ramnath_vaidya: Interactive slides from Interactive Visualization
        presentation #user2014. http://ramnathv.github.io/user2014-rcharts/#1"
)

ex_hash(x)
## [[1]]
## [1] "#rstats"  "#ggplot2"
## 
## [[2]]
## [1] "#magrittr" "#pipeR"    "#rstats"  
## 
## [[3]]
## [1] "#user2014"
ex_tag(x)
## [[1]]
## [1] "@hadley"
## 
## [[2]]
## [1] "@timelyportfolio"
## 
## [[3]]
## [1] "@ramnath_vaidya"
ex_url(x)
## [[1]]
## [1] NA
## 
## [[2]]
## [1] "http://renkun.me/r/2014/07/26/difference-between-magrittr-and-pipeR.html"
## 
## [[3]]
## [1] "http://ramnathv.github.io/user2014-rcharts/#1"

Extract Bracketed Text

y <- c("I love chicken [unintelligible]!", 
    "Me too! (laughter) It's so good.[interrupting]",
    "Yep it's awesome {reading}.", "Agreed. {is so much fun}")

ex_bracket(y)
## [[1]]
## [1] "unintelligible"
## 
## [[2]]
## [1] "laughter"     "interrupting"
## 
## [[3]]
## [1] "reading"
## 
## [[4]]
## [1] "is so much fun"
ex_curly(y)
## [[1]]
## [1] NA
## 
## [[2]]
## [1] NA
## 
## [[3]]
## [1] "reading"
## 
## [[4]]
## [1] "is so much fun"
ex_round(y)
## [[1]]
## [1] NA
## 
## [[2]]
## [1] "laughter"
## 
## [[3]]
## [1] NA
## 
## [[4]]
## [1] NA
ex_square(y)
## [[1]]
## [1] "unintelligible"
## 
## [[2]]
## [1] "interrupting"
## 
## [[3]]
## [1] NA
## 
## [[4]]
## [1] NA

Extract Numbers

z <- c("-2 is an integer.  -4.3 and 3.33 are not.",
    "123,456 is a lot more than -.2",
    "hello world -.q")
rm_number(z)
## [1] "is an integer. and are not." "is a lot more than"         
## [3] "hello world -.q"
ex_number(z)
## [[1]]
## [1] "-2"   "-4.3" "3.33"
## 
## [[2]]
## [1] "123,456" "-.2"    
## 
## [[3]]
## [1] NA
as_numeric(ex_number(z))
## [[1]]
## [1] -2.00 -4.30  3.33
## 
## [[2]]
## [1] 123456.0     -0.2
## 
## [[3]]
## [1] NA

Extract Times

x <- c(
    "I'm getting 3:04 AM just fine, but...",
    "for 10:47 AM I'm getting 0:47 AM instead.",
    "no time here",
    "Some time has 12:04 with no AM/PM after it",
    "Some time has 12:04 a.m. or the form 1:22 pm"
)
ex_time(x)
## [[1]]
## [1] "3:04"
## 
## [[2]]
## [1] "10:47" "0:47" 
## 
## [[3]]
## [1] NA
## 
## [[4]]
## [1] "12:04"
## 
## [[5]]
## [1] "12:04" "1:22"
as_time(ex_time(x))
## [[1]]
## [1] "00:03:04.0"
## 
## [[2]]
## [1] "00:10:47.0" "00:00:47.0"
## 
## [[3]]
## [1] NA
## 
## [[4]]
## [1] "00:12:04.0"
## 
## [[5]]
## [1] "00:12:04.0" "00:01:22.0"
as_time(ex_time(x), as.POSIXlt = TRUE)
## [[1]]
## [1] "2017-04-09 00:03:04 EDT"
## 
## [[2]]
## [1] "2017-04-09 00:10:47 EDT" "2017-04-09 00:00:47 EDT"
## 
## [[3]]
## [1] NA
## 
## [[4]]
## [1] "2017-04-09 00:12:04 EDT"
## 
## [[5]]
## [1] "2017-04-09 00:12:04 EDT" "2017-04-09 00:01:22 EDT"

Remove Non-Words & N Character Words

x <- c(
    "I like 56 dogs!",
    "It's seventy-two feet from the px290.",
    NA,
    "What",
    "that1is2a3way4to5go6.",
    "What do you*% want?  For real%; I think you'll see.",
    "Oh some <html>code</html> to remove"
)

rm_non_words(x)
## [1] "I like dogs"                                 
## [2] "It's seventy two feet from the px"           
## [3] NA                                            
## [4] "What"                                        
## [5] "that is a way to go"                         
## [6] "What do you want For real I think you'll see"
## [7] "Oh some html code html to remove"
rm_nchar_words(rm_non_words(x), "1,2")
## [1] "like dogs"                              
## [2] "It's seventy two feet from the"         
## [3] NA                                       
## [4] "What"                                   
## [5] "that way"                               
## [6] "What you want For real think you'll see"
## [7] "some html code html remove"

Functions in qdapRegex

Name Description
TC Upper/Lower/Title Case
cheat A Cheat Sheet of Common Regex Task Chunks
escape Escape Strings From Parsing
explain Visualize Regular Expressions
pastex Paste Regular Expressions
print.explain Prints a explain object
grab Grab Regular Expressions from Dictionaries
group Group Regular Expressions
group_or Group Wrap and `or` Concatenate Elements
is.regex Test Regular Expression Validity
rm_non_ascii Remove/Replace/Extract Non-ASCII
bind_or Boundary Wrap (Bind) and `or` Concatenate Elements
c.extracted Combines a extracted Object
regex_supplement Supplemental Canned Regular Expressions
regex_usa Canned Regular Expressions (United States of America)
rm_ Remove/Replace/Extract Function Generator
S Use C-style String Formatting Commands
bind Add Left/Right Character(s) Boundaries
qdapRegex qdapRegex: Regular Expression Removal, Extraction, & Replacement Tools for the
rm_city_state Remove/Replace/Extract City & State
rm_city_state_zip Remove/Replace/Extract City, State, & Zip
rm_hash Remove/Replace/Extract Hash Tags
rm_nchar_words Remove/Replace/Extract N Letter Words
rm_non_words Remove/Replace/Extract Non-Words
rm_time Remove/Replace/Extract Time
rm_title_name Remove/Replace/Extract Title + Person Name
rm_abbreviation Remove/Replace/Extract Abbreviations
rm_repeated_characters Remove/Replace/Extract Words With Repeating Characters
rm_repeated_phrases Remove/Replace/Extract Repeating Phrases
rm_caps Remove/Replace/Extract All Caps
rm_caps_phrase Remove/Replace/Extract All Caps Phrases
rm_emoticon Remove/Replace/Extract Emoticons
regex_cheat A dataset containing the regex chunk name, the regex string, and a
rm_between Remove/Replace/Extract Strings Between 2 Markers
rm_bracket Remove/Replace/Extract Brackets
rm_date Remove/Replace/Extract Dates
print.extracted Prints a extracted Object
print.regexr Prints a regexr Object
rm_citation Remove/Replace/Extract Citations
rm_citation_tex Remove/Replace/Extract LaTeX Citations
rm_percent Remove/Replace/Extract Percentages
rm_dollar Remove/Replace/Extract Dollars
rm_email Remove/Replace/Extract Email Addresses
rm_repeated_words Remove/Replace/Extract Repeating Words
rm_tag Remove/Replace/Extract Person Tags
rm_zip Remove/Replace/Extract Zip Codes
validate Regex Validation Function Generator
rm_number Remove/Replace/Extract Numbers
rm_endmark Remove/Replace/Extract Endmarks
rm_default Remove/Replace/Extract Template
rm_phone Remove/Replace/Extract Phone Numbers
rm_postal_code Remove/Replace/Extract Postal Codes
rm_url Remove/Replace/Extract URLs
rm_white Remove/Replace/Extract White Space
No Results!

Last month downloads

Details

Type Package
Date 2017-04-09
LazyData TRUE
License GPL-2
URL http://trinker.github.com/qdapRegex/
BugReports http://github.com/trinker/qdapRegex/issues
Collate 'S.R' 'bind.R' 'bind_or.R' 'c.extracted.R' 'case.R' 'cheat.R' 'utils.R' 'rm_default.R' 'escape.R' 'explain.R' 'grab.R' 'group.R' 'group_or.R' 'is.regex.R' 'pastex.R' 'print.extracted.R' 'print.regexr.R' 'qdapRegex-package.R' 'rm_.R' 'rm_abbreviation.R' 'rm_between.R' 'rm_bracket.R' 'rm_caps.R' 'rm_caps_phrase.R' 'rm_citation.R' 'rm_citation_tex.R' 'rm_city_state.R' 'rm_city_state_zip.R' 'rm_date.R' 'rm_dollar.R' 'rm_email.R' 'rm_emoticon.R' 'rm_endmark.R' 'rm_hash.R' 'rm_nchar_words.R' 'rm_non_ascii.R' 'rm_non_words.R' 'rm_number.R' 'rm_percent.R' 'rm_phone.R' 'rm_postal_code.R' 'rm_repeated_characters.R' 'rm_repeated_phrases.R' 'rm_repeated_words.R' 'rm_tag.R' 'rm_time.R' 'rm_title_name.R' 'rm_url.R' 'rm_white.R' 'rm_zip.R' 'validate.R'
RoxygenNote 6.0.1
NeedsCompilation no
Packaged 2017-04-09 19:12:59 UTC; Tyler
Repository CRAN
Date/Publication 2017-04-09 21:29:36 UTC
depends R (>= 3.1.0)
imports stringi (>= 0.5-5)
suggests testthat
Contributors Jason Gray

Include our badge in your README

[![Rdoc](http://www.rdocumentation.org/badges/version/qdapRegex)](http://www.rdocumentation.org/packages/qdapRegex)