From the course: R for Data Science: Lunch Break Lessons

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

regexpr

regexpr

- [Instructor] R provides support for regular expressions and it does this with two commands, regexpr and regmatches. Let's take a look at how those two work together to provide the support. To illustrate, I've created a vector called someText and into it I've placed two elements, just standard strings, and I'd like to find anything that begins with a B. So to do that, I'll type in reg expression, E-X-P-R, and I'll tell it that I want to search for the regexpr (b\\w+) and what I'm telling it is, any word that begins with B, followed by any word-like characters, and that's alpha-numeric A-B-C-D et cetera. I want to search through someText. Now, when I run this command, you'll see that I receive a list, six space three, and seven space six, and chars, and TRUE. Regexpr is telling me that I am matching something in both elements of someText. The first number, six, is a match in the first element. So, if I look at the…

Contents