In my last tutorial, I talked about some basic fundamentals.  Let's move on to narrowing down search scope some more.  Recall that previously, I had introduced:

< - Matches beginning of a word

> - Matches end of a word

Now, let's introduce a few more!

^ - Matches beginning of a line

$ - Matches end of a line

^n - Matches n occurrences

For this tutorial, I will be using the following text lines

Hello world!

How are you today?

I'm fine, thanks!

If we want matches to return words that begin with H, we can search with “<H[a-z]*”  This will match the following results (match highlighted in blue)

Hello world!

How are you today?

I'm fine, thanks!

What if you only want to find words that begin with H and has a total of 3 characters?  You can search with “<H[a-z]^2>”.  This matches a word that begins with H, followed by two letters in the range of a-z, and then the word ends.  This match will return the word “How”.  Note that if you didn't include the end of word match “>” and just searched with “<H[a-z]^2”, you will return the following matches:

Hello world!

How are you today?

I'm fine, thanks!

This is because we didn't specify that the word had to end, so any word that begins with H and then had two characters in the range of a-z will match.

As we searched for beginning of a word, you can also search for beginning of a line  For example, “^I'm” will return a match for any line that begins with “I'm”. 

Hello world!

How are you today?

I'm fine, thanks!

If you had wanted to return a match for the whole line, you can use “^I'm.*”  (Remember that . is any character except line breaks)

Similarly, you can return matches of end of line.  For example “!$” (Match line endings of !) will return the following matches:

Hello world!

How are you today?

I'm fine, thanks!

Again, to return a match for a whole line you can use .*!”

Hello world!

How are you today?

I'm fine, thanks!

Stay tuned for more regular expression tips to come! :)

-Fiona