Regular Expressions

In computer science, regular expressions are used to define a search pattern. Using a search pattern we can validate that a string matches our pattern or we can use our pattern to perform find and replace operations.

In this example we will use Regular Expressions to find and replace the world "lamb" with "puppy" in a string.

The IgnoreCase method means that our search will be case insensitive. The Global = True method means that it will find as many matches as it can and not just stop after finding the first match.

alt text

So the first message box that prints on line 12 looks like this:

alt text

And the second message box that prints on line 18 (after we've done the find and replace) looks like this:

alt text

Exercise

Since regular expressions can be a little tricky, there are lots of online resources to help you test your regular expressions. Like this one: http://www.regextester.com/ Use this site to get familiar.


Previous section: