14. Metacharacters: ^ and $
- So, let's look at some examples of using ^ and $.
- Consider the file
example02.txt:
This is another file with grey in it.
It's all about the colour gray
grey is all that seems to be in here
but don't get all gray about it!
- Now, let's use grep again:
grep '^gr[ea]y' example02.txt
- The result is:
grey is all that seems to be in here
- Similarly, if we use the regex:
grep 'gr[ea]y$' example02.txt
- The result is:
It's all about the colour gray
- Simple, isn't it?
- Confession time: It's not actually as simple as all that. Matching the start and end
of a line is all very well for when we grep the lines in a file, or when editing the
lines of a file in vi, but what happens when we read in an entire file into a single
variable in Perl? Where's the start and end of a line in that? Or is it now the start
and end of the target string, which is suddenly a file? What if the file has embedded
newlines in it?
- We'll come back to this after we've seen another metacharacter we need to know about first.
|