Erlang Central

Difference between revisions of "Matching Letters"

From ErlangCentral Wiki

 
(References)
(2 intermediate revisions by one user not shown)
Line 14:Line 14:
 
3> case regexp:match("Soup", "^[A-Za-z]+$") of
 
3> case regexp:match("Soup", "^[A-Za-z]+$") of
 
3>    {match,_,_} -> io:fwrite("I like alphabet soup!\n");
 
3>    {match,_,_} -> io:fwrite("I like alphabet soup!\n");
3>    true        -> io:fwrite("I don't like number soup.\n")
+
3>    nomatch    -> io:fwrite("I don't like number soup.\n")
 
3> end.                           
 
3> end.                           
 
I like alphabet soup!
 
I like alphabet soup!
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 ==
  
Your system's locale (3) manpage Mastering Regular Expressions
+
Your system's locale (3) manpage, [http://www.oreilly.com/catalog/regex2/ Mastering Regular Expressions]
  
 
[[Category:CookBook]][[Category:Regular Expressions]]
 
[[Category:CookBook]][[Category:Regular Expressions]]

Revision as of 06:31, 9 March 2007

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>    nomatch     -> 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