python regular expression la. (2 pts) Write a regular expression pattern that ma
ID: 3594192 • Letter: P
Question
python regular expression
la. (2 pts) Write a regular expression pattern that matches all numbers from 1-31 (interesting because it represents all the possible days in a month). It would be trivial to write a huge choice with 31 different numbers: (11213.120130131)$ requiring 87 characters 2 anchors ( and $), 2 parentheses, 30 | characters, 9 characters for single digit numbers, 20 characters for numbers 10-19, 20 characters for numbers 20-29, and 4 characters for 30 and 31. But you are required to write a shorter RE: look for commonalties. My solution is 22 characters (and I found many slightly longer variants) Put your answer in repatternl.txt.Explanation / Answer
import re
for i in range(0, 100):
if re.match(r'([1-9]|[12]d|3[0-1])$', str(i)):
print(i)
pattern is r'([1-9]|[12]d|3[0-1])$'
Copy pastable code link: https://paste.ee/p/iFNYo
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.