Jump to content

Regular expressions: Difference between revisions

no edit summary
No edit summary
Line 545:
hello HELLO world WORLD</pre>
 
=={{header|Elixir}}==
Elixir allows pattern matching using the <code>~r</code> sigil.
<lang Elixir>
str = "This is a string"
if str =~ ~r/string$/, do: IO.inspect "str ends with 'string'"
</lang>
A number of modifiers can be appended to the regular expression; <code>~r/pattern/i</code>, for instance, toggles case insensitivity.
<lang Elixir>
str =~ ~r/this/ # => false
str =~ ~r/this/i # => true
</lang>
Both <code>Regex</code> and <code>String</code> have a <code>replace</code> function.
<lang Elixir>
str1 = ~r/a/ |> Regex.replace(str,"another")
str2 = str1 |> String.replace(~r/another/,"even another")
</lang>
<code>Regex.replace</code> allows for a function to be used as a replacement value. A function can modify the found pattern.
<lang Elixir>
str3 = ~r/another/ |> Regex.replace(str2, fn x -> "#{String.upcase(x)}" end)
</lang>
 
{{out}}
str ends with 'string'<br>
false<br>
true<br>
"This is another string"<br>
"This is even another string"<br>
"This is even ANOTHER string"<br>
=={{header|Emacs Lisp}}==
<lang Emacs Lisp>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.