2

Is there a simple way to get a string representation of a regex? Passing a regex to IO.inspect prints out the string representation so I assume this must be possible in some way.

0

2 Answers 2

4

You can use Regex.source/1.

Regex.source(regex)

Returns the regex source as a binary.

Demo:

iex(1)> Regex.source(~r/[a-z]/)
"[a-z]"
Sign up to request clarification or add additional context in comments.

Comments

2

Acc. to Elixir Regex documentation you may use:

source(regex)
source(t) :: String.t
Returns the regex source as a binary.

opts(regex) -
opts(t) :: String.t
Returns the regex options as a string.

See Elixir demo:

rx = ~r/\(cat\d{8} #{"foo"}\)/i
IO.puts Regex.opts(rx)
IO.puts Regex.source(rx)  

Output:

i
\(cat\d{8} foo\)

Knowing both the pattern and modifiers, you can base your further actions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.