Find Plain Text Patterns using Regular Expressions

Joe Maddalone
InstructorJoe Maddalone
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 6 years ago

The simplest use of Regular Expressions is to find a plain text pattern. In this lesson we'll look at at finding plain text patterns as well as using the metacharacter "." and how to escape a metacharacter.

[00:00] Finding a plain text pattern with regular expressions is fairly trivial. So we're going to have a string here called, or it's going to read "Cat sat on the hat." If we wanted to find the pattern C-A-T we can just set up a regular expression to cat, save that, and we can see that we are in fact getting cat. If we wanted hat, we just change that there, if we wanted sat we save that there. Now if we wanted to find all of the lower case As followed by lower case t, we just use our global flag and we get the three lower case As follows by lower case Ts in our string.

[00:41] Now if we wanted to find all of the strings that are a character followed by at, we can use the metacharacter dot. Now you can see we've identified the three patterns that our regular expression represents, so we've got cat, sat, and hat. Now the dot metacharacter identifies any character including letters, digits, and a dash, but not a line break. So if I save that, you can see we've got a pattern on each and every one of these guys, including the non-breaking spaces.

[01:15] If I go up here and add some line breaks to our string, everything is going to work just fine. But what we can do is say we're looking for any character, followed by any character, followed by any character. If I save that, we get our cat, we get sat, for the O-N we get our non-breaking space and on, the we get T-H-E, and then after that we get a non-breaking space followed by H-A, so that's one pattern, then we've got a t and a dot, and it doesn't have a third character, so that is not a match.

[01:47] Also, if we add a fourth one here, cat no longer matches because the character after the t is a line break. Then if we go ahead and add a few more of those you can see that we get down to the last line which is the only line that's long enough to match our current pattern.

[02:05] Now if we wanted to match a literal dot in our string we need to escape it since the dot is a metacharacter in regular expressions. So to do that we just add backslash right before the metacharacter that we want to match as a literal character, and then you can see that we in fact match the dot in our string.

egghead
egghead
~ 31 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today