Regular expressions

Computer Science · Theory of computation

Question 1 of 10

0

    Regex notation: . is any one character, * is zero or more, + is one or more, ? is zero or one, [ ] is any one of the listed characters, | means or, ( ) groups, and ^ and $ mark the start and end of the whole string. The whole string must fit the pattern. How many of the strings below match the regular expression `^cg?d$`? Strings: cgg, cgd, cd, ccd, cg, ccgd.

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Regular expressions explained

    A regular expression, often shortened to regex, is a compact pattern that describes a set of strings. Regular expressions are used in search tools, form validation and text processing. A regular expression is built from a few symbols. A plain letter matches itself. A square bracket group such as [0-9] matches any one character from the set. A vertical bar means or, so a|b matches either a or b. A star means zero or more repeats of the item before it, a plus means one or more, and a question mark means optional. Patterns are written once and then applied quickly to large amounts of text.

    Anchors fix the pattern to the start or the end of the text. A caret at the start means the match must begin at the first character, and a dollar sign at the end means it must finish at the last character. Curly braces give an exact count, so [0-9]{3} matches exactly three digits.

    Worked example

    Consider the pattern that matches any string of 0s and 1s followed by a single 0, written (0∣1)∗0(0 \mid 1)^{*}0. Test the strings 1010, 11 and 100.

    • 1010 ends in 0, so it matches.
    • 11 ends in 1, so it does not match.
    • 100 ends in 0, so it matches.

    The pattern matches exactly the binary strings that end in 0.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      Regex notation: . is any one character, * is zero or more, + is one or more, ? is zero or one, [ ] is any one of the listed characters, | means or, ( ) groups, and ^ and markthestartandendofthewholestring.Thewholestringmustfitthepattern.Howmanyofthestringsbelowmatchtheregularexpression‘dg?c mark the start and end of the whole string. The whole string must fit the pattern. How many of the strings below match the regular expression `^dg?c`? Strings: cdgc, dcg, dgc, dc, ddc, dgcd.

    • Medium

      Regex notation: . is any one character, * is zero or more, + is one or more, ? is zero or one, [ ] is any one of the listed characters, | means or, ( ) groups, and ^ and markthestartandendofthewholestring.Astringmatchesonlyifthewholestringfitsthepattern.Howmanyofthefollowingstringsmatchtheregularexpression‘(hb∣g)+ mark the start and end of the whole string. A string matches only if the whole string fits the pattern. How many of the following strings match the regular expression `^(hb|g)+`? Strings: ggghb, ghb, ghbh, ggg, gggg, ghbg, hghb.

    • Hard

      Regex notation: . is any one character, * is zero or more, + is one or more, ? is zero or one, [ ] is any one of the listed characters, | means or, ( ) groups, and ^ and markthestartandendofthewholestring.Howmanydifferentstringsofexactly6characters,madeonlyoftheletterseanda,matchtheregularexpression‘e+(ae)∗ mark the start and end of the whole string. How many different strings of exactly 6 characters, made only of the letters e and a, match the regular expression `^e+(ae)*`?