首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >剪辑初学者:在python和clipspy的剪辑规则中添加常规exp或选择*的任何替代选项

剪辑初学者:在python和clipspy的剪辑规则中添加常规exp或选择*的任何替代选项
EN

Stack Overflow用户
提问于 2021-10-25 09:14:53
回答 1查看 61关注 0票数 0

我有一个剪辑规则,它匹配模板事实中给定的路径,如果路径匹配,则将id和与该路径相关联的文本断言到另一个模板中。路径只是字典中的一个文本条目。路径为"//Document/Sect2/P2“。我想制定一条规则,如下所示:

代码语言:javascript
复制
Pfad "//Document/Sect[*]/P[*]"

因此它可以与//Document/Sectany number here/Pany number here匹配。我找不到任何与此相关的东西,所以这是否可能,或者是否有其他选择?任何帮助都将不胜感激。谢谢!以下是我的规则代码:

代码语言:javascript
复制
rule3= """ 
        (defrule createpara
        (ROW (counter ?A) 
             (ID ?id)                  
             (Text ?text)
             (Path "//Document/Sect/P"))
        
        =>
        (assert (WordPR (counter ?A) 
                        (structure ?id) 
                        (tag "PAR") 
                        (style "Paragraph") 
                        (text ?text))))
        """
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-25 19:36:46

CLIPS不支持正则表达式,但是可以通过define_function方法自己添加对正则表达式的支持。

代码语言:javascript
复制
import re
import clips


RULE = """
(defrule example-regex-test
  ; An example rule using the Python function within a test
  (path ?path)
  ; You need to double escape (\\\\) special characters such as []
  (test (regex-match "//Document/Sect\\\\[[0-9]\\\\]/P\\\\[[0-9]\\\\]" ?path))
  =>
  (printout t "Path " ?path " matches the regular expression." crlf))
"""


def regex_match(pattern: str, string: str) -> bool:
    """Match pattern against string returning a boolean True/False."""
    match = re.match(pattern, string)

    return match is not None


env = clips.Environment()
env.define_function(regex_match, name='regex-match')
env.build(RULE)

env.assert_string('(path "//Document/Sect[2]/P[2]")')

env.run()
代码语言:javascript
复制
$ python3 test.py 
Path //Document/Sect[2]/P[2] matches the regular expression.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69705456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档