e r l a n g : c o o k b o o k

/ Erlang.RegexMatchingLetters

This Web


WebHome 
WebChanges 
TOC
NewRecipe 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Erlang Links


Erlang.org
Erlang Wiki
ErlMan
Erlang Wiki
The Jungerl
Erlang-fr.org
Joe Armstrong
Lambda the Ultimate

Erlang Web Ring


[Prev]: Joe Armstrong's Page
[Next]: Joe Armstrong's Page

Matching Letters

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

References

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


Comments about this recipe

Contributors

-- BrentAFulgham - 30 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: RegexRecipes
TopicOrder: 020

 
 
Copyright © 2004 by the contributing authors. All material on the Erlang Cookbook web site is the property of the contributing authors.
This material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest