gpt4 book ai didi

python - 如何使用 python 正则表达式将路由 key 与 RabbitMQ 主题交换的绑定(bind)模式相匹配?

转载 作者:行者123 更新时间:2023-12-02 00:29:33 25 4
gpt4 key购买 nike

我基本上是在 RabbitMQ 上工作。我正在编写一个 python 代码,其中我试图查看路由键是否与主题交换时的绑定(bind)模式匹配。我发现了这个链接-https://www.rabbitmq.com/tutorials/tutorial-five-java.html它说:“但是,绑定(bind)键有两个重要的特殊情况:

* (star) can substitute for exactly one word.

# (hash) can substitute for zero or more words.

那么如何将消息的路由键与队列的绑定(bind)模式匹配呢?例如,消息的路由键是“my.routing.key”,队列使用绑定(bind)模式“my.#.*”绑定(bind)到主题交换。一般来说,我如何匹配这些字符串模式进行主题交换,最好是使用 python 正则表达式。

最佳答案

这几乎是节点库的直接端口 amqp-match :

import re

def amqp_match(key: str, pattern: str) -> bool:
if key == pattern:
return True
replaced = pattern.replace(r'*', r'([^.]+)').replace(r'#', r'([^.]+.?)+')
regex_string = f"^{replaced}$"
match = re.search(regex_string, key)
return match is not None

关于python - 如何使用 python 正则表达式将路由 key 与 RabbitMQ 主题交换的绑定(bind)模式相匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com