Skip to content

Matching an exact amount of characters? #443

Answered by ptmcg
metaperl asked this question in Q&A
Discussion options

You must be logged in to vote

To specifically answer your question, Word can take min, max, and exact arguments to have more control over the number of matched characters.

We don't see this in us_phone_number because this is an ambiguous expression. It could match 123-4567 or 012-345-6789. To make this a little less ambiguous, we could write it as:

local_phone_number = Word(nums, exact=3) + '-' + Word(nums, exact=4)
us_phone_number = '(' + Word(nums, exact=3) + ')' + local_phone_number
phone_number = us_phone_number | local_phone_number

phone_number.run_tests("""\
(212)555-1212
234-5678
""")

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by metaperl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants