Regex Problem? Here’s an R package that will write Regex for you

[This article was first published on r-bloggers on Programming with R, 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.

REGEX is that thing that scares everyone almost all the time. Hence, finding some alternative is always very helpful and peaceful too. Here’s a nice R package thst helps us do REGEX without knowing REGEX.


This is the REGEX pattern to test the validity of a URL:

^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$

A typical regular expression contains — Characters ( http ) and Meta Characters ([]). The combination of these two form a meaningful regular expression for a particular task.
So, What’s the problem?

Remembering the way in which characters and meta-characters are combined to create a meaningful regex is itself a tedious task which sometimes becomes a bigger task than the actual problem of NLP which is the larger goal.

Solution at Hand

Some good soul on this planet has created an open-source Javascript library JSVerbalExpressions to make Regex creation easy. Then some other good soul (Tyler Littlefield) ported the javascript library to R— RVerbalExpressions. This is the beauty of the open source world.


is available on RVerbalExpressions Github so you can use devtools or remotes to install it from Github.

# install.packages("devtools")


Let’s create a pseudo-problem that we’d like to solve with regex through which we can understand this package to programmatically create regex.

A simpler one perhaps, We’ve got multiple text like and we’d like to extract the names from it. Here’s our input and output look like:

strings = c('123Problem233','233Solution434','223Tim Apple444')

Problem, Solution, Time Apple

Once we solve this, we’ll move forward with slightly complicated problems.


Before we code, it’s always good to write-out a pseudo-code on a napkin or even a paper if you’ve got. That is, We want to extract names (which is composition of alphabets) except numbers (which is digits). We build a regex for one-line and then we iterate it for all the elements in our vector.


Like any other R package, we can load RVerbalExpressions with library() function.


Constructing the Expression

Extract Strings

Like many other modern-day R packages, RVerbalExpressions support %>% pipe operator for better simplicity and readability of the code. But for this problem of extracting strings that are present between the numbers, we can simply use one function that is rx_alpha() to say that we need alphabets from the given string.

expr =  rx_alpha()


[1] "P" "r" "o" "b" "l" "e" "m"

[1] "S" "o" "l" "u" "t" "i" "o" "n"

[1] "T" "i" "m" "A" "p" "p" "l" "e"*

Extract Numbers

Similar to the text that we extracted, Extracting Numbers again is very English as we’ve to use the function rx_digit() to say that we need numbers from the given text.

expr =  rx_digit()

[1] "1" "2" "3" "2" "3" "3"

[1] "2" "3" "3" "4" "3" "4"

[1] "2" "2" "3" "4" "4" "4"

Another Constructor to extract the name as a word

Here, we can use the function rx_word() to match it as word (rather than letters).

expr =  rx_alpha()  %>%  rx_word() %>% rx_alpha()


[1] "Problem"

[1] "Solution"

[1] "Tim" "Apple"


What if we want to use the expression somewhere else or simply we need the regex expression. It’s simple because the expression is what we’ve constructed and printing what we constructed would reveal the relevant regex pattern.




Thus, we managed to build a regex pattern without knowing regex. Simply put, we programmatically generated a regex pattern using R (that doesn’t require the high-level knowledge of regex patterns) and accomplished a tiny task that we took up to demonstrate the potential. For more of Regex, Check out this course. The entire code is available here.

To leave a comment for the author, please follow the link and comment on their blog: r-bloggers on Programming with R. 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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)