Erlang Central

Difference between revisions of "Matching Letters"

From ErlangCentral Wiki

m (case...end is not if..end)
Line 36: Line 36:
== References ==
== References ==
Your system's locale (3) manpage Mastering Regular Expressions
Your system's locale (3) manpage, [ Mastering Regular Expressions]
[[Category:CookBook]][[Category:Regular Expressions]]
[[Category:CookBook]][[Category:Regular Expressions]]

Revision as of 06:31, 9 March 2007



You want to see if a value contains only alphabetic characters.


A first approximation can be achieved using the standard character classes:

1> regexp:first_match("OTPErlang", "^[A-Za-z]+$").
2> regexp:first_match("123-Have Fun", "^[A-Za-z]+$").
3> case regexp:match("Soup", "^[A-Za-z]+$") of
3>    {match,_,_} -> io:fwrite("I like alphabet soup!\n");
3>    nomatch     -> io:fwrite("I don't like number soup.\n")
3> end.                          
I like alphabet soup!


Unfortunately, this does not properly handle foreign languages that might have additional characters outside the standard 26 english letters.

4> regexp:first_match("Molière", "^[A-Za-z]+$").

If you need to match alternative alphabets (as defined by the user's locale settings), you will have to identify the proper character codes for the additional characters and add them to the search routine. Erlang's built-in regular expression support is pretty miserable here.

As discussed in other recipes, Erlang is fully capable of processing any Unicode string (because it stores strings as lists of integers). However, Erlang is really not fully Unicode compliant because its regular expression engine does not support characters outside the standard ASCII set.


Your system's locale (3) manpage, Mastering Regular Expressions