How to efficiently find the first matching regex from a (long) list of regexes? #1138
-
Hi, imagine a poor man's translation service. The input is a message from a well-known set of messages and the output should be the same message in another language. However, the input text can contain numbers. A (possibly stupid) idea is to extract these numbers with a regex. Random example: Now, in practice there can be many different messages that need to be translated, so the task is to find the first regex out of a long list that matches the input. Just iterating over these can be slow and I feel like there should be faster approaches for this. (And "just iterating" likely is fast enough anyway; I'm just curious about better solutions.) Thanks! P.S.: Yes, I know that this is not how one is supposed to implement localisation. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Does One thing that would make this question easier to answer is if you provided a code example implementing what you describe in prose. Something simple that captures the essence of what you're trying to do. But is ultimately something with some inputs and can be run by others. Then I could see if it makes sense to transform it to |
Beta Was this translation helpful? Give feedback.
Does
RegexSet
work for you? Note its limitations however.One thing that would make this question easier to answer is if you provided a code example implementing what you describe in prose. Something simple that captures the essence of what you're trying to do. But is ultimately something with some inputs and can be run by others. Then I could see if it makes sense to transform it to
RegexSet
or possibly something else entirely.