Strings and Pattern Matching / Now you try: Strings
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
We've had a pretty good look at regular expression syntax in this chapter. There is a lot more to learn in terms of extended regular expression syntax, and the usage of regular expressions that will only become natural once you've used them a fair bit. But we've had a good look at the basics from which you can build your skills in regular expressions. Both pattern matching and using regular expressions to replace. Now I've put together a couple of exercises, which you are welcome to try if you like, and equally you can ignore them if you want to. One of the privileges of listening to a CD rather than having a real tutor who can bother you if you don't do your homework. The exercises are these. The first one is to put together a regular expression, that makes a reasonable attempt at checking if an e-mail is syntactically valid. There's two words in this sentence that I'd really like to stress. The first one is reasonable and the second one is syntactically. The reason I say a reasonable attempt, is that regular expressions have been built that attempt to be comprehensive at checking the different kinds of e-mails that are valid. These regular expressions are in fact whole systems or modules, that takes into account the wide variety of different possible e-mail addresses, and attempt to check each possible permutation and combination. I am not expecting you to do this. All I am asking with this exercise is to make a stab at putting together a pattern that can check if an e-mail address fits the general description, the common sense description of what an e-mail address should look like. It should have an @ sign in the middle, there should be a domain, and so on. The other word I like to stress is syntactically. There's no way you can test with a regular expression that an e-mail exists or not. Short of sending an e-mail, often seeing if it bounces, is no real way that you can test that. So that's all we are testing here, just for the syntax, and only a basic check at that. The example I've put on the examples folder on the CD is a pretty basic one, and I will be the first to admit that it's not in any way comprehensive. If you can beat it, then by all means go ahead. The next example is to replace all instances of the numerals, 0 to 9, in a string of text with their full word versions - one, two and so on. I am not asking you again to do the really complex version of this exercise, such as looking at the number 11 and turning back into eleven and so on. Or to look at 10s, 100s, 1000s. All we are looking at is the individual numbers on their own, finding instances of those within the string and returning just the English equivalents.
Tutorial Information
| Course: | Perl Fundamentals |
| Author: | Joshua Mostafa |
| SKU: | 33403 |
| ISBN: | 1-9320-7215-2 |
| Release Date: | 2002-12-19 |
| Duration: | 7.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 