Scripting Wife Uses Regular Expressions to Match a String

Scripting Wife Uses Regular Expressions to Match a String

  • Comments 3
  • Likes

Summary: On the road to the 2011 Scripting Games, the Scripting Wife learns about regular expressions and string matching.

Weekend Scripter

Microsoft Scripting Guy, Ed Wilson, here. The Scripting Wife and I decided to head to Hilton Head Island (no relation to Paris Hilton; and therefore, no teacup Chihuahuas around) for the weekend. One of nice things about living in Charlotte, North Carolina is its proximity to lots of really cool places. Within a short drive we can be in the mountains, at one of two awesome beaches, a funky old town, or even in Mayberry RFD.

I am sitting on the deck this morning, overlooking the Atlantic Ocean and enjoying the sounds of the waves on the rocks. I have my laptop with me, and I have been hanging out on Twitter and Facebook for part of the morning. I am sipping a cup of English Breakfast Tea (bag not loose leaf this morning…I did not bring my teapot with me).

I hear the screen from the living room slide open, and to my surprise it is the Scripting Wife. She walks over to where I am sitting and plops down in the chair beside me. I wait for her to speak.

“I was looking over the 2011 Scripting Games study guide, and I noticed there is a section about regular expressions. I have two words for you.”

I waited. It is a favorite game of hers, particularly early in the morning. She will pause for a long time between sentences with the intention of tricking me into replying. Then she accuses me of interrupting her. She does not know that after (a whole bunch) years of marriage, I know all of her tricks.

I looked up at her and smiled.

“OK. You think you are really clever, don’t you? Think you have figured out all of my tricks, eh? I was not going to do that at all. What I was trying to do, before I was so rudely interrupted by your smirking, is to ask you, “What is a regular expression?’”

“Well around here, a regular expression is, ‘Can it Script Monkey?’ I said matter-of-factly.

“You are not funny. I don’t even know why you try to be funny,” she scolded. “Now can you tell me what a regular expression is, and why I would want to use one?”

“Regular expressions are actually their own language. In fact, there have been several books written about them. They can be horribly complex, but they can also be rather simple to use,” I began.

“What kind of geek-speak double talk is that…they can be complex, they can be easy,” she admonished.

“What I mean is that you can use a very simple regular expression pattern, or you can use a very complex regular expression pattern. The best way to do this is for you to type some examples. Open the Windows PowerShell console, and create a string that says, ‘This is a string,’ and assign it to the dollar sign a variable,” I instructed.

The Scripting Wife eagerly opened the Windows PowerShell console, and typed the following command (space) represents the Space bar, <enter> represents the Enter or Return key).

$a<space>=<space>“This is a string”<enter>

“OK, now type $ a, and view the contents of the variable,” I said.

“Why don’t you just say ‘type dollar sign a’? Dude, you always make things sound like a book,” she said with mock annoyance. Her Windows PowerShell console is shown here.

Image of code

“Very good,” I said with about 50% sincerity, hoping that she would not catch it.

“Watch it. Save your fake praises for some other occasion,” she warned.

“OK. Now use regular expressions to see if the word is is present in the string.”

“Oh, I know that one. I have seen the contains operator listed. I will try that.”

She typed the following command:

$a<space>–contains<space>“is”

“Well this is stupid,” she said, “It doesn’t work. It says False. But I can see that the word is is in the string. What’s up with that?”

“This is a common problem,” I began.

“Then fix it,” she chastised.

Contains is used to find an exact match in an array or collection. The Match operator is used with regular expression patterns to find matches in a string,” I said. “Create an array with the numbers 1, 2, 3, and 4 in it. Then use the contains operator to look for the number 2.”

She typed the following:

1,2,3,4<space>-contains<space>2<enter>

The output shown here appeared in her Windows PowerShell console.

PS C:\> 1,2,3,4 -contains 2

True

PS C:\>

“Now use the match operator to see if the word is is present in your string,” I said.

She typed the following command:

$a<space>-match<space>"is"<enter>

The following is the command and the associated output from her Windows PowerShell console session.

PS C:\> $a = "This is a string"

PS C:\> $a

This is a string

PS C:\> $a -match "is"

True

PS C:\>

“This is pretty cool,” the Scripting Wife admitted.

“Now use the Up arrow, and try a few more matches. Use matches such as is space, is a string, and is a str,” I suggested.

The Scripting Wife typed the following commands (she used the Up arrow and the Left arrow to quickly recall the previous commands, and to quickly edit the match commands).

PS C:\> $a -match "is "

True

PS C:\> $a -match "is a string"

True

PS C:\> $a -match "is a str"

True

PS C:\>

She then turned her laptop monitor and showed me her work. The Windows PowerShell console looked like the following image when she was finished.

Image of code

“Now I am going to teach you your first special regular expression character. Remember Bugs Bunny?” I playfully asked.

“That smart-aleck cartoon rabbit?” she queried.

“Yes, the waskley wabbit that was Elmer Fudd’s arch nemesis,” I affirmed.

“What does Bugs Bunny have to do with regular expressions?” she asked, regretting in advance the answer.

“Well, Bugs Bunny loved carrots. The special character I am going to show you is the caret,” I said.

“Ugh,” she moaned. “You make a better straight man.”

“The caret character is used in regular expressions as a place indicator. The caret marks the beginning of a string; therefore, it tells the engine to limit the match to the beginning of the string. An illustration will make this clear. Add the caret to your last match pattern. The caret is found above the 6 on your keyboard, and it goes at the very beginning of the string to be matched,” I said.

The Scripting Wife searched her keyboard for a few seconds until she saw the ^ symbol above the 6 key on her US English layout keyboard. Here is the command and the output she received.

PS C:\> $a -match "^is a str"

False

PS C:\>

“It said False. I guess that means it did not find it,” she queried.

“You got it. Now use the caret and look for the word This,” I said.

This time, the Scripting Wife used the Up arrow to retrieve the previous command, and quickly edited the command so that it would look for the word This at the beginning of the line. The following output is her typed command and its associated output.

PS C:\> $a -match "^This"

True

PS C:\>

“It says True. Well, that is just slicker than a Quebec sidewalk in January,” she said. “Here, take a look.”

The Scripting Wife’s Windows PowerShell console is shown in the following image.

Image of code

“I am outta here. Did you know this place has a spa?” she said.

“You don’t have any cash,” I said lamely.

“But I can charge it to the room. I think I deserve a massage, and a manicure, and a pedicure. I need to get into shape for the 2011 Scripting Games,” she said.

“Indeed you do,” I sighed. “Indeed you do.”

“Can it, Script Monkey,” the Scripting Wife called from down the hall.

Oops, I guess she heard me…

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I love reading the adventures of Scripting Wife!  I gotta agree with her on the carrot-caret lead, though.

    Can't wait to see what she'll get into next!

  • @Sarah

    That just makes out day! Sorry you did not like my bunny joke. It was, better than the one @Scripting Wife came up with (hint: diamonds are a womans best friend ... also wrong kind of carrot)