You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Regular expressions are a very compact DSL to generate DFAs, and can be intimidating at first. We cannot expect users to be fluent in this DSL.
How?
We can make this easier by designing a simple Python DSL that compiles into regular expressions. It can be as simple as defining functions that return a regex string:
This is in practice slightly more complex, as some characters need to be escaped in literal expressions (such as (), and we cannot expect users who are not familiar with regular expressions to know that. A solution would be to create a RegexStr object that abstracts this away, for instance:
defescape(string):
ifstring=="(":
returnf"\{string}"ifstring==")":
returnf"\{string}"else:
returnstringclassRegexStr(str):
def__add__(self, other):
ifisinstance(other, RegexStr):
returnRegexStr(f"{self}{other}")
else:
returnRegexStr(f"{self}{escape(other)}")
def__radd__(self, other):
ifisinstance(other, RegexStr):
returnRegexStr(f"{other}{self}")
else:
returnRegexStr(f"{escape(other)}{self}")
defexactly(quantity, *regex_strs):
regex_str=''.join(regex_strs)
ifregex_str=='':
raiseValueError('regex_strs argument must have at least one nonblank value')
returnRegexStr(regex_str+'{'+str(quantity) +'}')
print("("+exactly(3, "abc") +")")
This is however a very rough design, and I am open to alternatives.
The text was updated successfully, but these errors were encountered:
Why?
Regular expressions are a very compact DSL to generate DFAs, and can be intimidating at first. We cannot expect users to be fluent in this DSL.
How?
We can make this easier by designing a simple Python DSL that compiles into regular expressions. It can be as simple as defining functions that return a regex string:
This is in practice slightly more complex, as some characters need to be escaped in literal expressions (such as
(
), and we cannot expect users who are not familiar with regular expressions to know that. A solution would be to create aRegexStr
object that abstracts this away, for instance:This is however a very rough design, and I am open to alternatives.
The text was updated successfully, but these errors were encountered: