skip navigation
 
 
 
WebInSight
Previous Next

Regular Expressions

Basic Matching with Regular Expressions

Regular expressions can be used as tests to replace the == and != operators on strings that we learned about previously. Like these constructs, they are also used in conditional statements. We use the User.RegexMatch method and give it two parameters: the regular expression (a string), and the message to match against. The following is an example regular expression that will be true if the string variable input contains the word "hello" anywhere within it.

if (User.RegexMatch("hello", message)) 
{
	return "hi there!";
} 
else 
{
	return "Hello! Hello! Is anyone there?";
}

Here is the full source for this example: BasicRegexExampleBot.cs

Regular Expressions Exercise 1

To use your new knowledge of regular expressions, build the following Simon Says program. Your program should act as someone playing the game Simon Says. The person messaging the bot will be Simon. Your chatbot should respond "I did it!" if command contains the string "Simon Says" and it should respond "I will not do it, Simon-impersonator!" otherwise. If you get stuck, here is the solution: SimonSaysBot.cs.

More Powerful Regular Expressions

Regular expressions are powerful and can be used to test for more complicated constructions. For example, what if we want to test to see if our string contains either "Hi", "Hello", or "Howdy"? We can do this by creating a regular expression that matches any of these phrases. We do this by connecting the different options for strings to look for by the | character, resulting in the regular expression "Hi|Hello|Howdy". In code this looks like the following:

if (User.RegexMatch("Hi|Hello|Howdy", message)) 
{
	return "Success!";
} 
else 
{
	return "Failure.";
}

Here is the full source for this example: PowerfulRegex1Bot.cs

Sometimes, you want to optionally match part of the string. For instance, what if you wanted to select out "cow" and "cows" but not "cowboy"? You can use the question mark (?) to indicate that the part of the expression that came before the ? is optional. In this example, "cows?" says to definitely match the "cow" part and to optionally match the "s".

Regular expressions can also group parts of the expression, which is useful when used with optionally matching pieces of the regular expression. The following regular expression will match "George Bush", "George W. Bush", and "George W Bush", but not "GeorgeBush."

if (User.RegexMatch("George (W(.)? )?Bush", message)) 
{
	return "Success!";
} 
else 
{
	return "Failure.";
}

Here is the full source for this example: PowerfulRegex2Bot.cs

In the regular expression "George (W(.)? )?Bush", the characters in "W. " (notice the space after "W.") are grouped together using parentheses and a question mark is added after the closing parenthesis to indicate that the "W. " is optional. Also, the period after the W is optional as well, since it is in parentheses and there is a question mark after it.

Regular Expressions Exercise 2

As a final text of your mastery of regular expressions, create a chatbot so that it responds "Barnyard animal!" if the message received contains any of the following three barnyard animals "cow", "horse" and "pig" and their plurals "cows", "horses", "pigs". All other strings would be rejected. For example, the string "pigsty" would not be accepted. The chatbot should respond: "Not a barnyard animal...". Are there multiple ways to write a regular expression that matches these strings?

One possible solution: RegexExercise2Bot.cs

Advanced Regular Expressions

The syntax allowed in regular expressions is far to vast to be within the scope of this tutorial. If you're interested, check out the regular expression reference page.

Previous Next
 
This work is supported by the following National Science Foundation Grants: CNS-087508, CNS-0549481, IIS-0811884, IIS-0415273
Send comments to Richard Ladner