16. Metacharacters - ^, $, . and [^...]
- Remember we were going to come back and look at what ^ and $ matched in odd situations?
- Remember that . didn't match null for most tools, and also something else? Well, that
something else is an embedded newline character! But you guessed that anyway :-)
- Obviously, for tools like grep, vi embedded newlines are not relevant
concepts, as these are line-by-line tools. But other tools can treat things differently:
| Situation |
awk |
sed |
Perl |
Python |
GNU Emacs |
lex |
Tcl |
| Does ^ match at start of file? |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
| Does ^ match after an embedded newline? |
No |
No |
No |
Yes |
Yes |
Yes |
No |
| Does $ match at end of file? |
Yes |
Yes |
Yes |
Yes |
Yes |
No |
Yes |
| Does $ match before an embedded newline preceding the end of file? |
No |
No |
Yes |
Yes |
Yes |
Yes |
No |
| Does $ match before an embedded newline? |
No |
No |
No |
Yes |
Yes |
Yes |
No |
| Does . match an embedded newline? |
Yes |
Yes |
No |
No |
No |
No |
Yes |
| Does a negated character class [^...] match an embedded newline? |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Source: Mastering Regular Expressions, Jeffrey E. F. Friedl, O'Reilly, 1997
- Note: I'm informed that Python now uses Perl's regex engine, so Python should behave just like Perl these days.
|