I am basically working on RabbitMQ. I am writing a python code wherein I am trying to see if the routing key matches with the binding pattern in case of topic exchange. I came across this link- https://www.rabbitmq.com/tutorials/tutorial-five-java.html where it says- "However there are two important special cases for binding keys:
* (star) can substitute for exactly one word.
# (hash) can substitute for zero or more words.
So how do I match the routing key of message with binding pattern of queue? For example routing key of message is "my.routing.key" and the queue is bound to topic exchange with binding pattern - "my.#.*". In general, how do I match these string patterns for topic exchange, preferably I am looking to use python regex.
I have some Java code if that can help you
Pattern toRegex(String pattern) {
// case sensitive word
final String word = "[a-zA-Z]+";
// replace duplicate # (this makes things simpler)
pattern = pattern.replaceAll("#(?:\\.#)+", "#");
// replace *
pattern = pattern.replaceAll("\\*", word);
// replace #
// lone #
if ("#".equals(pattern)) return Pattern.compile("(?:" + word + "(?:\\." + word + ")*)?");
pattern = pattern.replaceFirst("^#\\.", "(?:" + word + "\\.)*");
pattern = pattern.replaceFirst("\\.#", "(?:\\." + word + ")*");
// escape dots that aren't escapes already
pattern = pattern.replaceAll("(?<!\\\\)\\.", "\\\\.");
return Pattern.compile("^" + pattern + "$");
}
maybe someone can translate that into python.