coding DFAs
Playing around with Discrete Finite Automata from a programming point of view.
RFC 793
finite automata simulators & tools
Aside :
and :
regex
^ start of string
$ end of string
. any single character except newline
* 0 or more of the preceding RE
+ 1 or more of the preceding RE
? 0 or 1 of the preceding RE
| in A|B matches either
(..) grouping
[..] character set definition
\ change meaning of next character in various ways
and many others
def regex_match(regex, word):
""" Return True if the regex matches the entire string, else False.
>>> regex_match("ab*", "abbb")
True
>>> regex_match("ab*", "cab")
False
>>> regex_match("ab*", "b")
False
>>> regex_match("(a|b)*", "abaabbaabbb")
True
>>> regex_match("()|2", "2")
True
>>> regex_match("()|2", "")
True
>>> regex_match("()|2", "22")
False
"""
import re
return bool(re.match('^(' + regex + ')$', word))
if __name__ == '__main__':
import doctest
doctest.testmod()
Come up with a regex for : 0's and 1's only, and a 1 in every 3rd position.
are these languages regular ?
- assigning names to numbers
alpha = 3 # good
beta = 123.23 # good
1 = 3 # bad (letters only on left)
a 3 # bad (no = sign)
1 + 2 / 3.4
1.2 + 3.2 + 42.3 + 3 * 17
1 + * 7 # bad
(1 + 2)/(7 - 3)
((3*7+1)/(1-3.2)) + (17/3)
((1-3) # bad - parens don't match