Regular Expressions / How to Create & Use Regular Expressions
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this video we're going to look at how we can actually use regular expressions in Ruby. And typically you're going to use regular expressions when you're searching a string for a particular value based on that regular expression that you've developed. And one of the common ones that you might use methods is the sub, or the Gsub, global sub, where you're actually substituting something, or you could use the index. So let's launch IRB here and create a string. That's got some nice regular patterns in there, and if we do take a look at um, this and let's look for the um, the first occurrences of the, of an O and followed by something and I'm going to close off. So there, there essentially we've used our, our pattern and it's going to tell us that 19 is where that occurs, where it's at the beginning of a word, so that would be this O right here. Another way we could use it, or another method we could use it with is, like I said, is the sub, and if we went in here and did that, we'll actually just find it. Let me go in here and do this; I think I typed that in wrong, but let me recopy this here; press start, oop. Good here, but you see where I messed up is I put a double quote on it; there we go. Now you can see it actually replaced, it substituted out, that particular item there. Now if we, see it didn't replace our string, our original string, now if we go in here and do global, Gsub, you'll see it replaced, well, just that one, but look at what my pattern is. If you've seen the other video that we introduced regular expressions you'll see that this is basically a place holder type thing and it stands for the ah, beginning, or not place holder, but anchor, this stands for the beginning of a word where there's an O and then another letter, and there's only one word in there: over. But, if we put in capital B and do that, now it's going to get rid of, out of fox and out of dog, those two letters, and if I go in here and pull that last guy off there, just get rid of the O's in there. Now here's where you can see a little bit more of the power, if I put in a whole, let's get rid of all the, the vowels in here, and now we've just eliminated all of the vowels, or whatever's matching in, in here. We can actually do a whole class where we just eliminate A through F, and so you can see A through F gets eliminated not just the vowels, but all the letters in between there. So that's a little bit of how to use regular expression type patterns in Ruby. Anywhere that you need something that you're going to search by, you can use a regular expression. Now there is another way that you can actually create a regular expression; instead of using this you can actually do the full version of it, which is essentially this. What you get with it by doing this, actually we've got to get rid of, like that, is that we can, after this we can specify different options here. I mean that you wouldn't put the actual word options there, but you could put different kinds of things that you'd want to be there, like ah, "ignore case," or something along those lines, so it's just a little bit different way of doing it. This will be accepted and I then can go in here and do this. Let's see if this works. There we go. There you get the same effect; um, maybe if you're building a program this might work a little bit better, but it's generally if you have options that you want to do something like that. So that's a little bit of using regular expressions and creating regular expressions with Ruby.
Tutorial Information
| Course: | Programming With Ruby |
| Author: | Al Anderson |
| SKU: | 33788 |
| ISBN: | 1-934743-01-1 |
| Release Date: | 2007-08-21 |
| Duration: | 8.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 