[erlang-questions] String pattern matching

Richard O'Keefe <>
Wed Apr 29 01:42:48 CEST 2009


On 29 Apr 2009, at 10:56 am, Kevin wrote:

>
> Does this behavior make sense to somebody or is this sugar tasting  
> sour?
>
>
> 25> "a"++B = "ab".
> "ab"
> 26> A++"b" = "ab".
> * 1: illegal pattern

100% sensible.

Fundamental rule: pattern matching only takes stuff apart;
it NEVER builds new lists or tuples.

To put it another way, patterns aren't regular expressions.

> I was just looking for an erlangy way to chomp the end of strings,  
> like
>
> NewLine ++ "\r\n" = Line

Well the *BEST* way to do that is to restructure the program
so that the \r\n never gets into the line in the first place.

The second best thing to do is to arrange that the rest of
the program doesn't _care_ whether the \r\n is there or not.
For example, if you are about to remove trailing spaces,
use instead something that removes trailing white space of
all kinds.

You can of course do
	"\n\r" ++ R = lists:reverse(Line),
	NewLine = lists:reverse(R)
which is easy to think of.





More information about the erlang-questions mailing list