In this movie, you'll get an overview of regular expressions.
- [Voiceover] Regular expressions is a pattern-matching language used in searching and replacing strings of text. Regular expressions became popular in the early days of UNIX, and they're used extensively in common UNIX utilities, like "sed" and "awk". Regular expressions are commonly referred to as "Regex" or "Regexp". Personally, I find "Regex" easier to pronounce without the p at the end. Perl uses regular expressions extensively, so it's a good idea to understand them well.
At first they may look a little intimidating, but they're surprisingly simple once you get a few basic concepts. This chapter is an introduction to regular expressions, and it's not meant to be exhaustive. It can be useful to gain a thorough understanding of the subject, but in practice, a few simple techniques will be sufficient for most purposes. This chapter will give you the foundation for using basic regular expressions in Perl. For further study, I recommend Jeffrey Friedl's excellent book, "Mastering Regular Expressions".
You'll find a link to the book at my Perl website, at perl.bw.org. Regular expressions use operators to search and replace strings of text. For example, this simple operator searches a string for a pattern match, and returns "true" or "false". This works with or without the leading "m" character. If you omit the m, you must use the slash symbol for your delimiter. If you include the m, you may use different delimiters as you can with the string quote operators.
The "s" operator is used for searching and replacing text. Like the match operator, you may substitute other symbols for the delimiters. This is common if you need to match the slash character in your regular expression. Finally, the QR operator is used to precompile a search pattern that you will use repeatedly. The match operator, represented by the equal and tilda symbols, is used to bind a variable or expression to a regular expression. This is used for both match and replace operations.
In a scalar context, the match operator returns a logical "true" if the match is found, or "false" if no match is found. There's also a negated version of the match operator. It returns "false" if a match is found, and "true" if no match is found. In a list context, the match operator returns a list of the matched parts of the string. You'll see a lot of examples of the match operator in this chapter. Regular expressions work by matching characters and wild cards.
A period matches any single character. For example, this will match either "wish" or "wash", but not "bash", because the "w" doesn't match. An asterisk matches zero or more instances of a given character. For example, this will match "wash", or "wash" with a bunch of extra "a"s, or, simply, "wsh", even though there's no "a" between the "w" and the "s". A plus sign matches one or more instances of a given character.
For example, this will match "book" or "bok", but not "bk", or "bak". Anchors are used to match strings that occur only at the start or end of a string. The carat anchors the start of a string. For example, this will match a "w" at the beginning of a string. And the dollar sign anchors the end of a string. For example, this will match a "w" at the end of a string. Perl uses modifiers to change the behavior of regular expressions.
For example, the "i" modifier tells the Regex to ignore the case of an input string. So this will match either all lowercase, or all uppercase, or any mixed case. Regular expressions are an essential part of the Perl language. They're deeply integrated into the language, and extremely useful. This chapter will cover the essentials of Perl regular expressions, and you'll see many examples throughout the rest of the course.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices
Skill Level Intermediate
Programming Foundations: Refactoring Codewith Simon Allardice1h 44m Intermediate
1. Setting Up
About Perl3m 36s
2. Quick Start
3. Basic Syntax
4. Values and Variables
7. Special Variables
9. Regular Expressions
11. References and Structures
12. File I/O
13. Built-In Functions
15. Best Practices
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.