[This article was first published on Numbers around us – Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.

In the intricate world of data analysis, the task of text pattern recognition and extraction is akin to unlocking a secret cipher hidden within ancient manuscripts. This is the realm of regular expressions (regex), a powerful yet often underappreciated tool in the data scientist’s toolkit. Much like the cryptex from Dan Brown’s “The Da Vinci Code,” which holds the key to unraveling historical and cryptic puzzles, regular expressions unlock the patterns embedded in strings of text data.

However, the power of regex comes at a cost — its syntax is notoriously complex and can be as enigmatic as the riddles solved by Robert Langdon in his thrilling adventures. For those not versed in its arcane symbols, crafting regex patterns can feel like deciphering a code without a Rosetta Stone. This is where the rebus package in R provides a lifeline. It simplifies the creation of regex expressions, transforming them from a cryptic sequence of characters into a readable and manageable code, akin to translating a hidden message in an old relic.

In this tutorial, we embark on a journey akin to that of Langdon’s through Paris and London, but instead of ancient symbols hidden in art, we’ll navigate through the complexities of text data. We will explore the fundamental principles of regex that form the backbone of text manipulation tasks. From basic pattern matching to crafting intricate regex expressions with the rebus package, this guide will illuminate the path towards mastering regex in R, making the process as engaging as uncovering a secret passage in an ancient temple.

Just as Langdon used his knowledge of symbolism to solve mysteries, we will use rebus to demystify regex in R, making this powerful tool accessible and practical for everyday data tasks. Whether you're a seasoned data scientist or a novice in the field, understanding how to effectively use regex is like discovering a hidden map that leads to buried treasure, providing you with the insights necessary to make informed decisions based on your data.

With our thematic setting now established, let us delve deeper into the world of regular expressions and reveal how the rebus package can transform your approach to data analysis, turning a daunting task into an intriguing puzzle-solving adventure.

In the quest for understanding regular expressions, akin to decoding a series of cryptic messages left in Leonardo Da Vinci’s artworks, we start with the very basics — the symbols and syntax that are the foundational tools of this powerful scripting language. Just as symbols held profound meanings in ancient scripts, each character in a regex pattern holds specific and significant implications.

Unveiling the Symbols

Regular expressions operate through special characters that, when combined, form patterns capable of matching and extracting text with incredible precision. Here are a few fundamental symbols to understand:

  • Dot (.): Like the omnipresent eye in a Da Vinci painting, the dot matches any single character, except newline characters. It sees all but the end of a line.
  • Asterisk (*): Mirroring the endless loops in a Fibonacci spiral, the asterisk matches the preceding element zero or more times, extending its reach across the string.
  • Plus (+): This symbol requires the preceding element to appear at least once, much like insisting on the presence of a key motif in an artwork.
  • Question Mark (?): It makes the preceding element optional, introducing ambiguity into the pattern, akin to an unclear symbol whose meaning might vary.
  • Caret (^): Matching the start of a string, the caret sets the stage much like the opening scene in a historical mystery.
  • Dollar Sign ($): This symbol matches the end of a string, providing closure and ensuring that the pattern adheres strictly to the end of the text.

Example: Simple Patterns in Action

Using the stringr library enhances readability and flexibility in handling regular expressions. Let’s apply this to find specific patterns:

library(stringr)
text_vector <- c("Secrets are hidden within.", "The key is under the mat.",
                      "Look inside, find the truth.", "Bridge is damaged by the storm")str_detect(text_vector, "bisb")
str_detect(text_vector, "bisb"

[1] FALSE  TRUE FALSE  TRUE

This code chunk check if word “is” is anywhere in given sentenece.

Crafting Your First Regex

To identify any word that ends with ‘ed’, signaling past actions, akin to uncovering traces of events long gone:

# Match words ending with 'ed'
str_extract(text_vector, "b[A-Za-z]+edb")

[1] NA        NA        NA        "damaged"

This expression uses b to ensure that 'ed' is at the end of the word, capturing complete words and not fragments—critical when every detail in a coded message matters.

Deciphering a Complex Regex

Let’s consider a more intricate regex pattern:

date_pattern <- "b(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)ddb"
# first check if pattern is present
str_detect("She was born on 12/08/1993, and he on 04/07/1989.", date_pattern)

[1] TRUE

# second extract the pattern
str_extract_all("She was born on 12/08/1993, and he on 04/07/1989.", date_pattern)

[[1]]
[1] "12/08/1993" "04/07/1989"

This regex looks extremely unfriendly at first glance, resembling an arcane code more than a helpful tool. It uses capturing groups, ranges, and alternations to accurately match dates in a specific format. Here’s the breakdown:

  • b: Word boundary, ensuring we match whole dates.
  • (0[1–9]|[12][0–9]|3[01]): Matches days from 01 to 31.
  • [- /.]: Matches separators which can be a dash, space, dot, or slash.
  • (0[1–9]|1[012]): Matches months from 01 to 12.
  • (19|20)dd: Matches years from 1900 to 2099.

This example shows how raw regex can quickly become complex and hard to follow, much like a cryptic puzzle waiting to be solved. The rebus package can help simplify these expressions, making them more accessible and easier to manage.

Building Blocks of Rebus

Just as Robert Langdon in “The Da Vinci Code” used his knowledge of symbology to decode complex historical puzzles, the rebus package in R enables us to build regular expressions from understandable components, transforming arcane syntax into legible code. This approach not only simplifies regex creation but also enhances readability and maintenance, making regex patterns as approachable as reading a museum guidebook.

Assembling the Codex

Rebus operates on the principle of constructing regex patterns piece by piece using function calls, which represent different regex components. This method aligns with piecing together clues from a scattered array of symbols to form a coherent understanding. Here are some of the building blocks provided by rebus:

  • digit(): Matches any number, simplifying digit recognition.
  • or(): Specifies a set of characters to match, allowing customization akin to selecting specific tools for a dig site.

Example: Email Pattern Construction with Rebus

Crafting an email validation pattern with rebus is akin to assembling a puzzle where each piece must fit precisely:

library(rebus)

# Define the pattern for a standard email
email_pattern <- START %R%
  one_or_more(WRD) %R% "@" %R%
  one_or_more(WRD) %R% DOT %R%
  or("com", "org", "net")

# Use the pattern to find valid emails
sample_text <- c("contact@example.com", "hello@world.net", "not-an-email")
str_detect(sample_text, email_pattern)

# short dictionary
%R% - special rebus operator for concatenation of elements
START - equivalent of ^
END - equivalent of $
WRD - treats characters as words similar to b in Regex
DOT - is a dot, I think nobody was confused here :D

This pattern, built with rebus functions, makes it easy to understand at a glance which components form the email structure, demystifying the regex pattern much like Langdon revealing the secrets behind a hidden inscription.

Deciphering Complex Text Patterns with Rebus

Consider a more complicated scenario where you need to validate date formats within a text. Using basic regex might involve a lengthy and cryptic pattern, but with rebus, we can construct it step-by-step:

# Define a pattern for dates in the format DD/MM/YYYY
date_pattern <-
  digit(2) %R% "/" %R%
  digit(2) %R% "/" %R%
  digit(4)

# Sample text for pattern matching
dates_text <- "Important dates are 01/01/2020 and 31/12/2020."

# First check if pattern can be found in text.
str_detect(dates_text, date_pattern)
[1] TRUE

# Then what it extracts.
str_extract_all(dates_text, date_pattern)
[[1]]
[1] "01/01/2020" "31/12/2020"

# But it will find also not proper dates like 32/13/2210 etc.

This example shows how rebus simplifies complex regex tasks, turning them into a series of logical steps, much like solving a riddle in an ancient tome.

But wait a minute… It is always a good idea to dig in documentation, and check out what can be found there.

dmy_pattern = DMY

str_detect(dates_text, dmy_pattern)
[1] TRUE

str_extract_all(dates_text, dmy_pattern)
[[1]]
[1] "01/01/2020" "31/12/2020"

# even shorter, even nicer, even faster

Tips for Crafting Expressions with Rebus

While rebus makes it easier to create and understand regex patterns, there are tips to further enhance your mastery:

  1. Start Simple: Begin with basic components and gradually add complexity.
  2. Test Often: Use sample data to test and refine your patterns frequently.
  3. Comment Your Code: Annotate your rebus expressions to explain the purpose of each component, especially in complex patterns.

Extracting Complex Medical Data from Clinical Notes

In the vein of a detective novel, akin to “The Da Vinci Code,” where each clue unravels a part of a larger mystery, this scenario involves deciphering clinical notes to extract specific medical information. This requires a keen understanding of the text’s structure and content, mirroring the precision needed to solve a cryptic puzzle left in an ancient artifact.

Setting the Scene: Medical Data Extraction Challenge

Clinical notes are packed with crucial medical details in a format that is often not standardized, making the extraction of specific information like medication prescriptions and patient diagnoses a complex task. Our goal is to develop regex patterns that can accurately identify and extract this information from varied text formats.

Step-by-Step Pattern Construction Using Rebus

Define Complex Patterns:

  • Medications often mentioned with dosages and frequencies.
  • Diagnoses that may include medical terms and conditions.
library(rebus)
library(stringr)

# Pattern for medication prescriptions
# Example format: [Medication Name] [Dosage in mg] [Frequency]
medication_pattern <- one_or_more(WRD) %R% SPACE %R% one_or_more(DGT) %R% "mg" %R% SPACE %R% one_or_more(WRD)

# Pattern for diagnoses
# Example format: Diagnosed with [Condition]
diagnosis_pattern <- "Diagnosed with " %R% one_or_more(WRD %R% optional(SPACE %R% WRD))

clinical_notes <- c("Patient was prescribed Metformin 500mg twice daily for type 2 diabetes.",
                    "Diagnosed with Chronic Heart Failure and hypertension.",
                    "Amlodipine 10mg once daily was recommended.",
                    "Review scheduled without any new prescriptions.")

Sample Clinical Notes:

clinical_notes <- c("Patient was prescribed Metformin 500mg twice daily for type 2 diabetes.",
                    "Diagnosed with Chronic Heart Failure and hypertension.",
                    "Amlodipine 10mg once daily was recommended.",
                    "Review scheduled without any new prescriptions.")

Extract and Validate Medical Data:

# Extracting medication details
medication_details <- str_extract_all(clinical_notes, medication_pattern)

# Extracting diagnoses
diagnoses_found <- str_extract_all(clinical_notes, diagnosis_pattern)

Example: Advanced Code Walkthrough

By running the above patterns against the clinical notes, we extract structured information about medications and diagnoses:

print(medication_details)

[[1]]
[1] "Metformin 500mg twice"

[[2]]
character(0)

[[3]]
[1] "Amlodipine 10mg once"

[[4]]
character(0)

print(diagnoses_found)

[[1]]
character(0)

[[2]]
[1] "Diagnosed with Chronic Heart Failure and hypertension"

[[3]]
character(0)

[[4]]
character(0)

This code extracts arrays containing detailed medication prescriptions and diagnosed conditions from each note, if available.

Handling Edge Cases and Variability

Medical terms and prescriptions can vary greatly:

  • Expand Vocabulary in Rebus: Include variations and synonyms of medical conditions and medication names.
  • Adjust for Complex Dosage Instructions: Medications might have dosages described in different units or intervals.

Mastering Medical Data Extraction

Just as each puzzle piece in “The Da Vinci Code” led to deeper historical insights, each regex pattern crafted with rebus reveals vital medical information from clinical notes, enabling better patient management and data-driven decision-making in healthcare.

Mastering Regex with Rebus for Complex Data Extraction

Navigating through complex data with regex and the rebus package is akin to deciphering hidden codes and symbols in a Dan Brown novel. Just as Robert Langdon uses his knowledge of symbology to unravel mysteries in “The Da Vinci Code,” data scientists and analysts use regex patterns crafted with rebus to unlock the mysteries within their data sets. This guide has shown how rebus transforms an intimidating script into a manageable and understandable set of building blocks, enabling precise data extraction across various domains, from legal documents to medical records.

Final Thoughts: The Art of Regex Crafting

  1. Iterative Development: Like solving a cryptic puzzle, developing effective regex patterns often requires an iterative approach. Start with a basic pattern, test it, refine it based on the outcomes, and gradually incorporate complexity as needed.
  2. Comprehensive Testing: Ensure your regex patterns perform as expected across all possible scenarios. This includes testing with diverse data samples to cover all potential variations and edge cases, mirroring the meticulous verification of clues in a historical investigation.
  3. Documentation and Comments: Regex patterns, especially complex ones, can quickly become inscrutable. Document your patterns and use comments within your rebus expressions to explain their purpose and structure. This practice ensures that your code remains accessible not just to you but to others who may work on it later, much like leaving a detailed map for those who follow in your footsteps.
  4. Stay Updated: Just as new archaeological discoveries can change historical understandings, advancements in programming and new versions of packages like rebus can introduce more efficient ways to handle data. Keeping your skills and knowledge up to date is crucial.
  5. Share Knowledge: Just as scholars share their discoveries and insights, sharing your challenges and solutions in regex with the community can help others. Participate in forums, write blogs, or give talks on your regex strategies and how you’ve used rebus to solve complex data extraction problems.

Strategies for Employing rebus Effectively

  • Utilize rebus Libraries: Leverage the full suite of rebus functionalities by familiarizing yourself with all its helper functions and modules. Each function is designed to simplify a specific aspect of regex pattern creation, which can drastically reduce the complexity of your code.
  • Pattern Modularity: Build your regex patterns in modular chunks using rebus, similar to constructing a narrative or solving a multi-part puzzle. This approach not only simplifies the development and testing of regex patterns but also enhances readability and maintenance.
  • Advanced Matching Techniques: For highly variable data, consider advanced regex features like lookaheads, lookbehinds, and conditional statements, which can be integrated into your rebus patterns. These features allow for more dynamic and flexible pattern matching, akin to adapting your hypothesis in light of new evidence.

Epilogue: The Power of Clarity in Data Parsing

In conclusion, mastering rebus and regex is like becoming fluent in a secret language that opens up vast archives of data, ready to be explored and understood. This guide has equipped you with the tools to start this journey, providing the means to reveal the stories hidden within complex datasets, enhance analytical accuracy, and drive insightful decisions.

Just as every clue solved brings Langdon closer to the truth in “The Da Vinci Code,” each pattern you decipher with rebus brings you closer to mastering the art of data. The path is laid out before you—begin your adventure, solve the puzzles, and unlock the potential of your data with confidence.

Appendix: The Regex Rosetta Stone — A Comprehensive Reference Guide

This appendix is designed as a quick yet comprehensive reference guide to using the rebus package for crafting regex expressions in R. Here you will find a brief description of some of the most pivotal functions, character classes, ready-made patterns, and interesting trivia on less commonly used regex features.

1. Most Common Functions in rebus

Let’s explore some of the essential rebus functions that you can use to construct regex patterns more intuitively:

  • or(): Combines multiple patterns and matches any of them. Useful for alternatives in a pattern.
  • exactly(): Specifies that the preceding element should occur an exact number of times.
  • literal(): Treats the following string as literal text, escaping any special regex characters.
  • optional(): Indicates that the preceding element is optional, matching it zero or one time.
  • zero_or_more(): Matches zero or more occurrences of the preceding element.
  • one_or_more(): Matches one or more occurrences of the preceding element.
  • lookahead(): Checks for a match ahead of the current position without consuming characters.
  • lookbehind(): Asserts something to be true behind the current position in the text.
  • repeated(): Matches a specified number of repetitions of the preceding element.
  • whole_word(): Ensures that the pattern matches a complete word.

2. Most Common Character Classes

Character classes simplify the specification of a set of characters to match:

  • DGT (Digit): Matches any digit, shorthand for digit().
  • ALNUM (Alphanumeric): Matches any alphanumeric character.
  • LOWER: Matches any lowercase letter.
  • UPPER: Matches any uppercase letter.
  • SPECIALS: Matches any special characters typically found on a keyboard.
  • ROMAN: Matches Roman numerals.
  • PUNCT (Punctuation): Matches any punctuation character.
  • NOT_DGT (Not Digit): Matches any character that is not a digit.
  • HEX_DIGIT (Hexadecimal Digit): Matches hexadecimal digits (0-9, A-F).
  • KATAKANA, HIRAGANA: Matches characters from the Japanese Katakana and Hiragana scripts.
  • HEBREW, CYRILLIC, ARABIC: Matches characters from the Hebrew, Cyrillic, and Arabic scripts.

3. Ready Patterns

rebus also includes functions for common pattern templates:

  • YMD: Matches dates in Year-Month-Day format.
  • TIME: Matches time in HH:MM:SS format.
  • AM_PM: Matches time qualifiers AM or PM.
  • CURRENCY_SYMBOLS: Matches common currency symbols.
  • HOUR12: Matches hour in 12-hour format.

4. Interesting But Less Used Character Classes (Trivia)

Explore some unique and less commonly used character classes:

  • DOMINO_TILES: Matches Unicode representations of domino tiles.
  • PLAYING_CARDS: Matches Unicode characters representing playing cards.

These unique character classes add a fun and often surprising depth to regex capabilities, allowing for creative data parsing and matching scenarios, much like uncovering an unexpected twist in a puzzle or story.

By familiarizing yourself with these tools, you can significantly enhance your ability to analyze and manipulate data effectively, transforming complex text into structured and insightful information. Keep this guide handy as a reference to navigate the vast landscape of regex with confidence and precision.

Final Tip:

If you haven’t already noted it, there is one small trick that will help you make step from using rebus to use “vanilla” regular expressions. When you place pattern in variable in your environment it is storing it as real RegExp, so if you would like to see it, and maybe use it directly in code, just print it to console.

# Imagine that there is some official number that consists of following parts
# Date in format YYYYMMDD, then letter T, then time in format HHMMSS and indicator AM or PM
# Looks pretty simple, and indeed is using rebus

pattern = YMD %R% "T" %R% HMS %R% AM_PM

# Now look to raw RegExp version.
print(pattern)
[0-9]{1,4}[-/.:, ]?(?:0[1-9]|1[0-2])[-/.:, ]?(?:0[1-9]|[12][0-9]|3[01])T(?:[01][0-9]|2[0-3])[-/.:, ]?[0-5][0-9][-/.:, ]?(?:[0-5][0-9]|6[01])(?:am|AM|pm|PM)

valid =  "20180101T120000AM"

str_detect(valid, pattern)
#[1] TRUE


The Rebus Code: Unveiling the Secrets of Regex in R was originally published in Numbers around us on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: Numbers around us – Medium.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you’re looking to post or find an R/data-science job.


Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.

Continue reading: The Rebus Code: Unveiling the Secrets of Regex in R

Read the original article