gge*_*ond 7 python regex porting racket
我正在尝试学习Racket,并在此过程中尝试重写Python过滤器.我的代码中有以下一对函数:
def dlv(text):
"""
Returns True if the given text corresponds to the output of DLV
and False otherwise.
"""
return text.startswith("DLV") or \
text.startswith("{") or \
text.startswith("Best model")
def answer_sets(text):
"""
Returns a list comprised of all of the answer sets in the given text.
"""
if dlv(text):
# In the case where we are processing the output of DLV, each
# answer set is a comma-delimited sequence of literals enclosed
# in {}
regex = re.compile(r'\{(.*?)\}', re.MULTILINE)
else:
# Otherwise we assume that the answer sets were generated by
# one of the Potassco solvers. In this case, each answer set
# is presented as a comma-delimited sequence of literals,
# terminated by a period, and prefixed by a string of the form
# "Answer: #" where "#" denotes the number of the answer set.
regex = re.compile(r'Answer: \d+\n(.*)', re.MULTILINE)
return regex.findall(text)
Run Code Online (Sandbox Code Playgroud)
从我可以看出,Racket中第一个函数的实现将是以下几点:
(define (dlv-input? text)
(regexp-match? #rx"^DLV|^{|^Best model" text))
Run Code Online (Sandbox Code Playgroud)
哪个似乎工作正常.在处理第二个函数的实现时,我目前已经提出以下内容(开始):
(define (answer-sets text)
(cond
[(dlv-input? text) (regexp-match* #rx"{(.*?)}" text)]))
Run Code Online (Sandbox Code Playgroud)
这是不正确的,因为regexp-match*
它给出了与正则表达式匹配的字符串列表,包括花括号.有谁知道如何获得与Python实现相同的行为?此外,任何关于如何使正则表达式"更好"的建议将非常感激.
你很近.您只需添加#:match-select cadr
到您的regexp-match
通话中:
(regexp-match* #rx"{(.*?)}" text #:match-select cadr)
Run Code Online (Sandbox Code Playgroud)
默认情况下,#:match-select
其值为car
,返回整个匹配的字符串.cadr
选择第一个组,caddr
选择第二个组等.有关详细信息,请参阅regexp-match*
文档.