I'm implementing an Android application using Mqtt (Paho). I have some components where I have to compare generally the subscribed topic which includes wildcards with the acutal topic that a client has published to.
I just don't get my head around it and my "rudimentary" Regex-skills don't helpt either...
Is there a utility class in Paho/Java MQTT to see if
"SENSOR/TEMPERATURE/+/DEGREE/#/ID" (subscribed topic)
is applies to
"SENOR/TEMPERATURE/GARDEN/DEGREE/CELSIUS/ABOVEZERO/ID" (actual topic)?
Does somebody know what would be the best way to do that?
Thank you!
EDIT: Hi There - I think this should be the right
public static boolean compareTopic(final String actualTopic, final String subscribedTopic){
return actualTopic.matches(subscribedTopic.replaceAll("\\+", "[^/]+").replaceAll("#", ".+"));
}
Your solution should work so in the case of your example the regular expression would be
A website like http://www.regexplanet.com/advanced/java/index.html is a very good resource for cases like this.