Ïðèãëàøàåì ïîñåòèòü
Ãîðüêèé (gorkiy-lit.ru)

Q&A

Previous Table of Contents Next

Q&A

Q1:

The pattern /\W(\w)+\W/ doesn't seem to match all the words on the line, just the ones in the middle. Why?

A1:

You're looking for word characters surrounded by nonword characters. The first word of the line—assuming it starts at the beginning of the line—doesn't have a nonword character in front of it. It doesn't have a character in front of it at all.

Q2:

What's the difference between m// and //? I don't get it.

A2:

There's almost no difference at all. The only difference is that if you decide to specify a pattern delimiter other than /, you can do so only if you precede the pattern with an m—for example, m!pattern!.

Q3:

I'm trying to verify that the user typed a number, but /\d*/ doesn't seem to work. It always returns true!

A3:

It returns true because a pattern using only the * quantifier always succeeds. It might match zero occurrences of \d, or it might match 2 or 100 or 1,000. Using /\d+/ ensures that you have at least one digit.

    Previous Table of Contents Next