nats-io / nats.net.v1

The official C# Client for NATS
Apache License 2.0
646 stars 151 forks source link

Export match function #852

Closed Shawak closed 9 months ago

Shawak commented 10 months ago

Proposed change

Use case

I ran into a problem where I have to check if the subject I am receiving matches a specific wildcard subject, I searched for a function to check this but I couldnt find any

Contribution

No response

scottf commented 10 months ago

So I know there is similar logic already to this in the server as it has to be able to resolve a non-wildcard subject to the proper stream/subject. That code is pretty complex. Also, you know more about your subjects and wildcards so could build an efficient function with that knowledge. If you would like, you are welcome to build this and we'll consider adding it to the project.

Shawak commented 9 months ago

I'm now using this to solve my issue:

    var regex = new Regex($"^{Regex.Escape(subject).Replace(@"\*", "[^.]+").Replace(@">", ".+")}$", RegexOptions.Compiled);