RegexToStrings – Get the strings a regex will match

This library allows you to get all the strings that a regex can match.


import RegexToStrings

regex_to_strings("11|2[25]2") == {:ok, ["11", "222", "252"]}

regex_to_strings!("8(024|53)9") == ["80249", "8539"]

regex_to_strings!("(16|7?[56])24") == ["1624", "524", "7524", "624", "7624"]

regex_to_strings!("1[03-59]") == ["10", "13", "14", "15", "19"]

regex_to_strings("1.+2") == :unsupported_regex

regex_to_strings!("1.+2") # RuntimeError: unsupported metacharacter "."

Aren’t there infinitely many matches in some (most) cases?

1 Like

Can you add a link to the library? :slight_smile:

1 Like

Oops, ty! Done!

assert_raise RuntimeError, "unsupported metacharacter \".\"", fn ->

assert regex_to_strings("1.+2") == :unsupported_regex

Ty updated the examples to reflect that:)

Maybe you should rebrand this library to something like string_generator because technically you are not actually supporting regexes. You are supporting a reduced subset of them.

The original use case is generating a list of possible values from a regex, so I keep the name because personally my goal was not to generate strings.

For example a Jamaican international number starts with β€œ1” and then β€œ658|876” according to Google’s libphonenumber. This library allows me to have the list of the possible values, i.e. β€œ1658”, β€œ1876”.

However it’s true that it could be used to generate a non-trivial series of numbers/strings:)