E.g:
arg = "echo:hello"
prefix = "echo"
case arg do
<<^prefix, ":", msg::binary>> -> IO.puts("Echo message: #{msg}")
_ -> IO.puts("No match")
end
Result: No match
What if I want to use the value of prefix
as a pattern match?
Pattern matching is used to determine whether source files of high-level languages are syntactically correct. It is also used to find and replace a matching pattern in a text or code with another text/code. Any application that supports search functionality uses pattern matching in one way or another.
The binary string matching problem consists in finding all the occurrences of a pattern in a text where both strings are built on a binary alphabet. This is an interesting problem in computer science, since binary data are omnipresent in telecom and computer network applications.
Patterns are written in JME syntax, but there are extra operators available to specify what does or doesn't match. The pattern-matching algorithm uses a variety of techniques to match different kinds of expression.
C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches. The " is expression" supports pattern matching to test an expression and conditionally declare a new variable to the result of that expression.
This won't work because you can only match fixed size binaries if you're not matching the "rest" of the string. There are 3 different solutions, depending on your use-case
If you really want to use that binary pattern matching, you can manually calculate the size beforehand:
arg = "echo:hello"
prefix = "echo"
prefix_size = byte_size(prefix)
case arg do
<<^prefix::binary-size(prefix_size), ":", msg::binary>> -> IO.puts("Echo message: #{msg}")
_ -> IO.puts("No match")
end
Depending on your use case, you could use module attributes, which size is known at compile time so they do work:
defmodule MyModule do
@prefix "echo"
def test(arg) do
case arg do
<<@prefix::binary, ":", msg::binary>> -> IO.puts("Echo message: #{msg}")
_ -> IO.puts("No match")
end
end
end
Or, if you want to keep prefix
a runtime binary, you could use String.replace_prefix/3
arg = "echo:hello"
prefix = "echo"
case String.replace_prefix(arg, "#{prefix}:", "")do
^arg -> IO.puts("No match")
msg -> IO.puts("Echo match: #{msg}")
end
String.replace_prefix/3
returns the input string if there is no match found, so we match it via ^arg
. If this is not the case, we got a match and since we replaced the prefix with ""
, we just get the part after the :
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With