regex to match positive integers
create and test a regex to: match positive integer numbers
### useful: test data
good_matches = [
"1",
"12",
"65324",
"0",
]
bad_matches = [
"blah",
"f123",
"245fsd"
"4-5",
"4.0"
"-10"
]
import re
regex = r"\b[0-9]+\b"
for test in good_matches:
if not re.fullmatch(regex, test):
print("failed:", test)
for test in bad_matches:
if re.fullmatch(regex, test):
print("failed:", test)
print(regex)
regex for positive or negative integers
create and test a regex to: match positive or negative integers
### useful: test data
good_matches = [
"1",
"12",
"65324",
"+19",
"-10",
"0"
]
bad_matches = [
"-0",
"+0",
"blah",
"f123",
"123you",
"4-5",
"4.0"
]
regex = r"([+\-]?[1-9][0-9]*)|0"
for test in good_matches:
if not re.fullmatch(regex, test):
print("failed:", test)
for test in bad_matches:
if re.fullmatch(regex, test):
print("failed:", test)
print(regex)
decimal numbers *
create and test a regex to: match decimal numbers
### useful: test data
good_matches = [
"1",
"12",
"123.4554",
"0.43",
"-10",
"0",
"+2.3",
"-2.3"
]
bad_matches = [
"-0",
"+0",
"blah",
"f123",
"123you",
"4-5",
"..34",
".34",
"0.",
"234."
]
regex = r"([+-]?[0-9]+\.[0-9]+)|([+\-]?[1-9][0-9]*)|0"
for test in good_matches:
if not re.fullmatch(regex, test):
print("failed:", test)
for test in bad_matches:
if re.fullmatch(regex, test):
print("failed:", test)
print(regex)
verbs
create and test a regex to: find words that look like verbs and end in ‘-ing’
### useful: test data
good_matches = [
"jumping",
"eating",
'sleeping',
'ting',
'bing',
'zipziplinging'
]
bad_matches = [
'jump',
'ing',
'jump ing',
'_ing',
'234ing'
]
regex = r"[a-zA-Z]+ing"
for test in good_matches:
if not re.fullmatch(regex, test):
print("failed:", test)
for test in bad_matches:
if re.fullmatch(regex, test):
print("failed:", test)
print(regex)
cats are awesome
create and test a regex to match: lines that start with the word ‘cats’ and end with the words ‘are awesome’
### useful: test data
good_matches = [
"cats are awesome",
"cats that sleep are awesome",
'cats that can count to 10 are awesome',
'cats 1234567890-/.,gdf/.g, are awesome',
]
bad_matches = [
'cat'
'awesome',
'cats are',
'cats are awesome too',
'my cats are awesome',
]
regex = r'cats.*are awesome'
for test in good_matches:
if not re.fullmatch(regex, test):
print("failed:", test)
for test in bad_matches:
if re.fullmatch(regex, test):
print("failed:", test)
print(regex)
TODO
TODO: excersizes for iterall and match groups