Skip to main content
Top

2021 | OriginalPaper | Chapter

8. Elements of Regular Expressions

Author : William Yarberry

Published in: CRAN Recipes

Publisher: Apress

Activate our intelligent search to find suitable subject content or patents.

search-config
loading …

Abstract

Literals are simply characters themselves, such as “a” or “boat” or “123.” Some characters are “reserved” with special meanings, such as “+.” In the case of the plus sign, its special meaning is “additional characters like one just to the left of the + sign.” If you want to use any of these reserved characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1\\+1=2. Otherwise, the plus sign has a special meaning. Remember that two backslashes are required.

              string1 <- "This is elementary Watson. 1+1=2"
              my.regex <- "1\\+1=2"
              my.regex.replacement.value <- "two plus two equals four "
              sub(pattern = my.regex,replacement =
                my.regex.replacement.value,x = string1)
             
              ## [1] "This is elementary Watson. two plus two equals four "
            

Dont have a licence yet? Then find out more about our products and how to get one now:

Springer Professional "Wirtschaft+Technik"

Online-Abonnement

Mit Springer Professional "Wirtschaft+Technik" erhalten Sie Zugriff auf:

  • über 102.000 Bücher
  • über 537 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Maschinenbau + Werkstoffe
  • Versicherung + Risiko

Jetzt Wissensvorsprung sichern!

Springer Professional "Technik"

Online-Abonnement

Mit Springer Professional "Technik" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 390 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Maschinenbau + Werkstoffe




 

Jetzt Wissensvorsprung sichern!

Springer Professional "Wirtschaft"

Online-Abonnement

Mit Springer Professional "Wirtschaft" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 340 Zeitschriften

aus folgenden Fachgebieten:

  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Versicherung + Risiko




Jetzt Wissensvorsprung sichern!

Footnotes
1
“Regular Expressions Quick Start,” accessed on March 23, 2020, www.regular-expressions.info/quickstart.html.
 
2
“R: RegEx for Containing Pattern with Negation,” Stack Overflow, accessed on March 26, 2020, https://stackoverflow.com/questions/46898699/r-regex-for-containing-pattern-with-negation.
 
3
“RegEx – Regular Expression Negation in R,” Stack Overflow, accessed on March 26, 2020, https://stackoverflow.com/questions/59139892/regular-expression-negation-in-r.
 
4
“RegEx – Extract Capture Group Matches from Regular Expressions? (Or: Where Is Gregexec?),” Stack Overflow, accessed on March 26, 2020, https://stackoverflow.com/questions/18620571/extract-capture-group-matches-from-regular-expressions-or-where-is-gregexec.
 
5
“R: Extract Matched Groups from a String,” accessed on March 26, 2020, https://jangorecki.gitlab.io/data.cube/library/stringr/html/str_match.html.
 
6
“RegEx – Common Operators,” accessed on March 26, 2020, http://web.mit.edu/gnu/doc/html/regex_3.html.
 
7
“RegEx Tutorial – Look ahead and Look behind Zero-Length Assertions,” accessed on March 16, 2020, www.regular-expressions.info/lookaround.html.
 
8
“Regular Expressions,” accessed on March 17, 2020, https://stringr.tidyverse.org/articles/regular-expressions.html.
 
9
“R: Regular Expressions as Used in R,” accessed on March 18, 2020, https://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html.
 
Metadata
Title
Elements of Regular Expressions
Author
William Yarberry
Copyright Year
2021
Publisher
Apress
DOI
https://doi.org/10.1007/978-1-4842-6876-6_8

Premium Partner