Erlang Central

Difference between revisions of "Matching Letters"

From ErlangCentral Wiki

 
(typo)
Line 32:Line 32:
 
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.
 
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 srings 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.
+
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.
  
 
== References ==
 
== References ==

Revision as of 17:49, 24 September 2006

Contents

Problem

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

Solution

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

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

Discussion

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]+$").
nomatch

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.

References

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