All Projects → tidyverse → Stringr

tidyverse / Stringr

Licence: other
A fresh approach to string manipulation in R

Programming Languages

r
7636 projects

Projects that are alternatives of or similar to Stringr

js-utils
A collection of dependency-free JavaScript utilities 🔧
Stars: ✭ 22 (-94.46%)
Mutual labels:  strings
Anymatch
‼️ Matches strings against configurable strings, globs, regular expressions, and/or functions
Stars: ✭ 289 (-27.2%)
Mutual labels:  regular-expression
Minta
✳️  Electron app for generating regular expressions
Stars: ✭ 353 (-11.08%)
Mutual labels:  regular-expression
Cuerdas
String manipulation library for Clojure(Script)
Stars: ✭ 272 (-31.49%)
Mutual labels:  strings
Repren
Rename anything
Stars: ✭ 275 (-30.73%)
Mutual labels:  regular-expression
Interview Questions In Javascript
A mostly reasonable collection of technical software development interview questions solved in Javascript
Stars: ✭ 3,268 (+723.17%)
Mutual labels:  strings
mattermost-plugin-autolink
Automatically rewrite text matching a regular expression into a markdown link.
Stars: ✭ 100 (-74.81%)
Mutual labels:  regular-expression
Regexp2
A full-featured regex engine in pure Go based on the .NET engine
Stars: ✭ 389 (-2.02%)
Mutual labels:  regular-expression
Rex
Your RegEx companion.
Stars: ✭ 283 (-28.72%)
Mutual labels:  regular-expression
Commit Watcher
Find interesting and potentially hazardous commits in git projects
Stars: ✭ 345 (-13.1%)
Mutual labels:  regular-expression
String Theory
Identify and reduce memory used by duplicate .NET strings
Stars: ✭ 273 (-31.23%)
Mutual labels:  strings
Re Flex
The regex-centric, fast lexical analyzer generator for C++ with full Unicode support. Faster than Flex. Accepts Flex specifications. Generates reusable source code that is easy to understand. Introduces indent/dedent anchors, lazy quantifiers, functions for lex/syntax error reporting, and more. Seamlessly integrates with Bison and other parsers.
Stars: ✭ 274 (-30.98%)
Mutual labels:  regular-expression
Generex
A Java library for generating String from a regular expression.
Stars: ✭ 316 (-20.4%)
Mutual labels:  regular-expression
Git Hound
Git plugin that prevents sensitive data from being committed.
Stars: ✭ 269 (-32.24%)
Mutual labels:  regular-expression
Androidlocalizeplugin
🌏 Android localization plugin. support multiple languages, no need to apply for key.
Stars: ✭ 352 (-11.34%)
Mutual labels:  strings
pcre-net
PCRE.NET - Perl Compatible Regular Expressions for .NET
Stars: ✭ 114 (-71.28%)
Mutual labels:  regular-expression
Regex
Regular expressions for swift
Stars: ✭ 306 (-22.92%)
Mutual labels:  regular-expression
Picomatch
Blazing fast and accurate glob matcher written JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
Stars: ✭ 393 (-1.01%)
Mutual labels:  regular-expression
Subconverter
Utility to convert between various subscription format
Stars: ✭ 4,912 (+1137.28%)
Mutual labels:  regular-expression
Lexmachine
Lex machinary for go.
Stars: ✭ 335 (-15.62%)
Mutual labels:  regular-expression

stringr

CRAN status R build status Codecov test coverage Lifecycle: stable

Overview

Strings are not glamorous, high-profile components of R, but they do play a big role in many data cleaning and preparation tasks. The stringr package provide a cohesive set of functions designed to make working with strings as easy as possible. If you’re not familiar with strings, the best place to start is the chapter on strings in R for Data Science.

stringr is built on top of stringi, which uses the ICU C library to provide fast, correct implementations of common string manipulations. stringr focusses on the most important and commonly used string manipulation functions whereas stringi provides a comprehensive set covering almost anything you can imagine. If you find that stringr is missing a function that you need, try looking in stringi. Both packages share similar conventions, so once you’ve mastered stringr, you should find stringi similarly easy to use.

Installation

# Install the released version from CRAN:
install.packages("stringr")

# Install the cutting edge development version from GitHub:
# install.packages("devtools")
devtools::install_github("tidyverse/stringr")

Cheatsheet

Usage

All functions in stringr start with str_ and take a vector of strings as the first argument.

x <- c("why", "video", "cross", "extra", "deal", "authority")
str_length(x) 
#> [1] 3 5 5 5 4 9
str_c(x, collapse = ", ")
#> [1] "why, video, cross, extra, deal, authority"
str_sub(x, 1, 2)
#> [1] "wh" "vi" "cr" "ex" "de" "au"

Most string functions work with regular expressions, a concise language for describing patterns of text. For example, the regular expression "[aeiou]" matches any single character that is a vowel:

str_subset(x, "[aeiou]")
#> [1] "video"     "cross"     "extra"     "deal"      "authority"
str_count(x, "[aeiou]")
#> [1] 0 3 1 2 2 4

There are seven main verbs that work with patterns:

  • str_detect(x, pattern) tells you if there’s any match to the pattern.

    str_detect(x, "[aeiou]")
    #> [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
    
  • str_count(x, pattern) counts the number of patterns.

    str_count(x, "[aeiou]")
    #> [1] 0 3 1 2 2 4
    
  • str_subset(x, pattern) extracts the matching components.

    str_subset(x, "[aeiou]")
    #> [1] "video"     "cross"     "extra"     "deal"      "authority"
    
  • str_locate(x, pattern) gives the position of the match.

    str_locate(x, "[aeiou]")
    #>      start end
    #> [1,]    NA  NA
    #> [2,]     2   2
    #> [3,]     3   3
    #> [4,]     1   1
    #> [5,]     2   2
    #> [6,]     1   1
    
  • str_extract(x, pattern) extracts the text of the match.

    str_extract(x, "[aeiou]")
    #> [1] NA  "i" "o" "e" "e" "a"
    
  • str_match(x, pattern) extracts parts of the match defined by parentheses.

    # extract the characters on either side of the vowel
    str_match(x, "(.)[aeiou](.)")
    #>      [,1]  [,2] [,3]
    #> [1,] NA    NA   NA  
    #> [2,] "vid" "v"  "d" 
    #> [3,] "ros" "r"  "s" 
    #> [4,] NA    NA   NA  
    #> [5,] "dea" "d"  "a" 
    #> [6,] "aut" "a"  "t"
    
  • str_replace(x, pattern, replacement) replaces the matches with new text.

    str_replace(x, "[aeiou]", "?")
    #> [1] "why"       "v?deo"     "cr?ss"     "?xtra"     "d?al"      "?uthority"
    
  • str_split(x, pattern) splits up a string into multiple pieces.

    str_split(c("a,b", "c,d,e"), ",")
    #> [[1]]
    #> [1] "a" "b"
    #> 
    #> [[2]]
    #> [1] "c" "d" "e"
    

As well as regular expressions (the default), there are three other pattern matching engines:

  • fixed(): match exact bytes
  • coll(): match human letters
  • boundary(): match boundaries

RStudio Addin

The RegExplain RStudio addin provides a friendly interface for working with regular expressions and functions from stringr. This addin allows you to interactively build your regexp, check the output of common string matching functions, consult the interactive help pages, or use the included resources to learn regular expressions.

This addin can easily be installed with devtools:

# install.packages("devtools")
devtools::install_github("gadenbuie/regexplain")

Compared to base R

R provides a solid set of string operations, but because they have grown organically over time, they can be inconsistent and a little hard to learn. Additionally, they lag behind the string operations in other programming languages, so that some things that are easy to do in languages like Ruby or Python are rather hard to do in R.

  • Uses consistent function and argument names. The first argument is always the vector of strings to modify, which makes stringr work particularly well in conjunction with the pipe:

    letters %>%
      .[1:10] %>% 
      str_pad(3, "right") %>%
      str_c(letters[2:11])
    #>  [1] "a  b" "b  c" "c  d" "d  e" "e  f" "f  g" "g  h" "h  i" "i  j" "j  k"
    
  • Simplifies string operations by eliminating options that you don’t need 95% of the time.

  • Produces outputs than can easily be used as inputs. This includes ensuring that missing inputs result in missing outputs, and zero length inputs result in zero length outputs.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].