[ Team LiB ] Previous Section Next Section

Workshop

Quiz

1:

What regular expression function would you use to match a pattern in a string?

2:

What regular expression syntax would you use to match the letter "b" at least once but not more than six times?

3:

How would you specify a character range between "d" and "f?"

4:

How would you negate the character range you defined in question 3?

5:

What syntax would you use to match either any number or the word "tree?"

6:

What regular expression function would you use to replace a matched pattern?

7:

The regular expression


.*bc

will match greedily; that is, it will match "abc000000bc" rather than "abc." How would you make the preceding regular expression match only the first instance of a pattern it finds?

8:

What backslash character will match whitespace?

9:

What function could you use to match every instance of a pattern in a string?

10:

Which modifier would you use in a PCRE function to match a pattern independently of case?


Answers

A1:

You can use the preg_match() function to find a pattern in a string.

A2:

You can use braces containing the minimum and maximum instances (the bounds) of a character to match:

b{1,6}

A3:

You can specify a character range using square brackets:

[d-f]

A4:

You can negate a character range with the caret symbol:

[^d-f]

A5:

You can match alternative branches with the pipe (|) character:

[0-9] |tree

A6:

You can use the preg_replace() function to replace a matched pattern with a given alternative.

A7:

By adding a question mark to a quantifier, you can force the match to be nongreedy:

/.*?bc/

A8:

\s will match whitespace in a PCRE.

A9:

The preg_match_all() function will match every instance of a pattern in a string.

A10:

The /i modifier will make a PCRE function match independently of case.


    [ Team LiB ] Previous Section Next Section