<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="150176" data-post-id="150176">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dkuku" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dkuku/120/15613_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dkuku
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p></p><div class="lightbox-wrapper"><a class="lightbox" href="https://forum.elixirforum.com/uploads/default/original/3X/4/2/421bffafbe2f059b7345d727b9fd1978f4957a6a.png" data-download-href="https://forum.elixirforum.com/uploads/default/421bffafbe2f059b7345d727b9fd1978f4957a6a" title="pm-erlang" rel="nofollow"><img src="https://forum.elixirforum.com/uploads/default/original/3X/4/2/421bffafbe2f059b7345d727b9fd1978f4957a6a.png" alt="pm-erlang" data-base62-sha1="9qPv0zyU1Yt1zFNGiBmYO8LFc0i" width="531" height="275"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pm-erlang</span><span class="informations">531×275 62.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
is there a way to use guard clauses from erlang ??<br>
I just found this in erlang masterclass course from kent university and I’m trying to solve the problems in elixir - looks similiar as op<p></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="150176" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-pattern-match-or-guards-if-a-string-is-alphanumeric/22556/9">Post #8</a>
	                </div>
	            </div>
              <div id="likers-container-150176" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="150176"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #8"></div>
  </section>
</div>
    <div class="postbit" id="140178" data-post-id="140178">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mudasobwa" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mudasobwa/120/5298_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mudasobwa
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Cure</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="al2o3cr" data-post="4" data-topic="22556">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/48/3457_2.png" class="avatar"> al2o3cr:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule RegexRecursion do
  defp call_bar("0" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("1" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("2" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("3" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("4" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("5" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("6" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("7" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("8" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("9" &lt;&gt; rest), do: call_bar(rest)
end
</code></pre>
</blockquote>
</aside>
<p>The above might be written as:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule RegexRecursion do
  Enum.each(?0..?9, fn char -&gt;
    defp call_bar(&lt;&lt;unquote(char), rest :: binary&gt;&gt;), do: call_bar(rest)
  end)
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="140178" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-pattern-match-or-guards-if-a-string-is-alphanumeric/22556/8">Post #7</a>
	                </div>
	            </div>
              <div id="likers-container-140178" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="140178"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #7"></div>
  </section>
</div>
    <div class="postbit" id="130409" data-post-id="130409">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kip" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/120/1440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kip
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>ex_cldr Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I have a package called <a href="https://hex.pm/packages/ex_cldr_unicode" rel="nofollow">ex_cldr_unicode</a> that includes guards for</p>
<ul>
<li><a href="https://hexdocs.pm/ex_cldr_unicode/Cldr.Unicode.Guards.html#is_currency_symbol/1" rel="nofollow">is_currency_symbol/1</a></li>
<li><a href="https://hexdocs.pm/ex_cldr_unicode/Cldr.Unicode.Guards.html#is_digit/1" rel="nofollow">is_digit</a></li>
<li><a href="https://hexdocs.pm/ex_cldr_unicode/Cldr.Unicode.Guards.html#is_lower/1" rel="nofollow">is_lower</a></li>
<li><a href="https://hexdocs.pm/ex_cldr_unicode/Cldr.Unicode.Guards.html#is_upper/1" rel="nofollow">is_upper</a></li>
</ul>
<p>These all operate on the <a href="https://en.wikipedia.org/wiki/Unicode_character_property" rel="nofollow">Unicode character classes</a> so it covers what passes for a digit in a more complete sense.  It might help or give you some ideas.</p>
<p>Note that it works on code points since there is a limited set of underlying functions that can be used in guards.</p>
<p>There is another bunch of functions that might be helpful, including <code>Cldr.Unicode.alphanumeric?/1</code> which will return a boolean and also uses the full Unicode definitions (not just Latin1):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; Cldr.Unicode.alphanumeric? "1st"
true

iex&gt; Cldr.Unicode.alphanumeric? "KeyserSöze1995"
true

iex&gt; Cldr.Unicode.alphanumeric? "3段"                 
true
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="130409" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-pattern-match-or-guards-if-a-string-is-alphanumeric/22556/7">Post #6</a>
	                </div>
	            </div>
              <div id="likers-container-130409" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="130409"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #6"></div>
  </section>
</div>
    <div class="postbit" id="130283" data-post-id="130283">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="henrique-marcomini-m" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/henrique-marcomini-m/120/14874_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  henrique-marcomini-m
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Well I think there is no pretty solution here. Though Hauleth presented something that would work ,I now see that the best way is through a well crafted regex. Thank you all for the answers.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="130283" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-pattern-match-or-guards-if-a-string-is-alphanumeric/22556/6">Post #5</a>
	                </div>
	            </div>
              <div id="likers-container-130283" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="130283"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #5"></div>
  </section>
</div>
    <div class="postbit" id="129228" data-post-id="129228">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hauleth" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/120/18942_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hauleth
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>TBH I would say that Regex is overkill there. And your pattern match can be improved:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def somefun(&lt;&lt;num&gt;&gt; &lt;&gt; rest) when num in ?0..?9, do: call_bar(rest)
def somefun(""), do: bar()
def somefun(bin) when is_binary(bin), do: foo()
</code></pre>
<p>The problem with regular expressions the use backtracking engine (and PCRE is such engine) is that it can explode to <code>O(n^2)</code> with some expressions (namely nested wildcard matches). And <a href="https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016" rel="noopener nofollow ugc">such cases have been spotted on the wild with pretty simple expressions</a>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="129228" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-pattern-match-or-guards-if-a-string-is-alphanumeric/22556/5">Post #4</a>
	                </div>
	            </div>
              <div id="likers-container-129228" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="129228"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #4"></div>
  </section>
</div>
    <div class="postbit" id="129113" data-post-id="129113">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="al2o3cr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/120/3457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  al2o3cr
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="henrique-marcomini-m" data-post="1" data-topic="22556">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/henrique-marcomini-m/48/14874_2.png" class="avatar"> henrique-marcomini-m:</div>
<blockquote>
<p>But I also want to use pattern matching or guards instead of a case. Is this even possible?</p>
</blockquote>
</aside>
<p>It’s <em>possible</em>, especially if the regular expression is simple - you can translate the regex into its corresponding finite state machine and represent <em>that</em> with pattern matching:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule RegexRecursion do
  def somefun(arg) do
    call_bar(arg)
  end

  defp call_bar("0" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("1" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("2" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("3" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("4" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("5" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("6" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("7" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("8" &lt;&gt; rest), do: call_bar(rest)
  defp call_bar("9" &lt;&gt; rest), do: call_bar(rest)

  defp call_bar(""), do: bar()
  defp call_bar(x) when is_binary(x), do: foo()

  defp foo(), do: IO.puts("foo")
  defp bar(), do: IO.puts("bar")
end
</code></pre>
<p>Here, the regex <code>[^\d]</code> translates to a state machine that stays in <code>call_bar</code> as long as each character is <code>0-9</code>, actually calls bar given an empty string, and calls foo otherwise.</p>
<p>If the regex doesn’t involve backreferences or lookaheads (so it’s a theory-of-languages regular expression), it’s <a href="https://en.wikipedia.org/wiki/Regular_language" rel="nofollow">always possible</a> to do this.</p>
<p><strong>HOWEVER</strong></p>
<p>The example above is a good example of how this approach obfuscates what should have been code like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule RegexRecursionSimple do
  def somefun(arg) when is_binary(arg) do
    if Regex.match?(~r/[^\d]/, arg) do
      foo()
    else
      bar()
    end
  end

  defp foo(), do: IO.puts("foo")
  defp bar(), do: IO.puts("bar")
end
</code></pre>
<p>I’m very curious what’s motivating the preference for pattern matching here; it’s not the right tool for the job.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="129113" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-pattern-match-or-guards-if-a-string-is-alphanumeric/22556/4">Post #3</a>
	                </div>
	            </div>
              <div id="likers-container-129113" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="129113"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #3"></div>
  </section>
</div>
    <div class="postbit" id="129095" data-post-id="129095">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="peerreynders" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/120/5826_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  peerreynders
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Probably not what you are looking for:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def somefun(true),
  do: foo()

def somefun(false),
  do: bar()

def somefun(arg) when is_binary(arg),
  do: Regex.match?(~r/[^\d]/, arg)
      |&gt; somefun()
</code></pre>
<p>or</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp p_somefun(true),
  do: foo()

defp p_somefun(false),
  do: bar()

def somefun(arg) when is_binary(arg) do
  Regex.match?(~r/[^\d]/, arg)
  |&gt; p_somefun()
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="129095" data-batch-url="/posts/batch_likers">
                        6
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-pattern-match-or-guards-if-a-string-is-alphanumeric/22556/3">Post #2</a>
	                </div>
	            </div>
              <div id="likers-container-129095" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="129095"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #2"></div>
  </section>
</div>
    <div class="postbit" id="129091" data-post-id="129091">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="NobbZ" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/NobbZ/120/27235_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  NobbZ
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>No, there is no way to make this regex or even something semantically equivalent into a guardsave predicate.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="129091" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-pattern-match-or-guards-if-a-string-is-alphanumeric/22556/2">Post #1</a>
	                </div>
	            </div>
              <div id="likers-container-129091" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="129091"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-first-post cat-first-post" title="First post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>