Match across lines with Elixir Regex `dotall` (s)
Elixir Regex is PCRE compliant and Ruby Regex isn't in at least one specific way. The m
(multiline) flag behaves differently in Elixir and Ruby.
Without any modifiers, Regex does not match across new lines:
iex> Regex.scan( ~r/.*/, "a\nb")
[["a"], [""], ["b"], [""]]
iex> Regex.scan( ~r/.*/, "ab")
[["ab"], [""]]
With the m
(multiline) modifier, Regex will match the beginning of each line when using the ^
char.
iex> Regex.scan( ~r/.*/m, "a\nb")
[["a"], [""], ["b"], [""]]
iex> Regex.scan( ~r/^.*/, "a\nb")
[["a"]]
iex> Regex.scan( ~r/^.*/m, "a\nb")
[["a"], ["b"]]
The s
(dotall) modifier is the right way in Elixir to match across newlines.
iex> Regex.scan( ~r/.*/s, "a\nb")
[["a\nb"], [""]]
iex> Regex.scan( ~r/^.*/s, "a\nb")
[["a\nb"]]
In ruby, you can match across lines with the m
(modifier) and the s
is ignored.
irb> "a\nb".scan(/.*/)
=> ["a", "", "b", ""]
irb> "a\nb".scan(/.*/m)
=> ["a\nb", ""]
irb> "a\nb".scan(/.*/s)
=> ["a", "", "b", ""]
Read about the history of dotall
here