Theory: Does the expression describe the word?

Test membership in the language of a regular expression.

What to build

A program that reads a regular expression and several words and decides each word.

Requirements

  • A word is in the language only if the whole word matches.
  • Print yes or no for each word.

Input

Line 1: the expression (letters, |, *, brackets). Line 2: n. Then n words (a word may be an empty line, meaning ε).

Output

n lines, each yes or no.

Constraints

The expression has at most 40 characters; at most 20 words of at most 40 symbols.

Example

Input:

(a|b)*abb
3
abb
abba
babb

Output:

yes
no
yes

Why it works

The language of an expression is the set of strings it matches completely, which is what fullmatch tests.

Examples

Example 1 · Input
(a|b)*abb
3
abb
abba
babb
Expected output
yes
no
yes
Example 2 · Input
(ab)*
3

aba
abab
Expected output
yes
no
yes

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

import re, then use re.fullmatch(pattern, word).

Hint 2

fullmatch returns None when the whole word does not match.

Solution

Show one correct solution (try it yourself first)
import re

pattern = input()
for _ in range(int(input())):
    word = input()
    print("yes" if re.fullmatch(pattern, word) else "no")

Lesson: Regular expressions →