Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if a string is alphanumeric in erlang

I am collecting tweets from twitter using erlang and I am trying to save only the hashtags to a database. However when I'm converting the bitstrings to list-strings all the non-latin-letter tweets converts to strange symbols. Is there any way to check if a string is only containing alphanumeric characters in erlang?

like image 855
Tilion Avatar asked Mar 14 '23 00:03

Tilion


2 Answers

for latin chars you can use this function:

is_alpha([Char | Rest]) when Char >= $a, Char =< $z ->
    is_alpha(Rest);
is_alpha([Char | Rest]) when Char >= $A, Char =< $Z ->
    is_alpha(Rest);
is_alpha([Char | Rest]) when Char >= $0, Char =< $9 ->
    is_alpha(Rest);
is_alpha([]) ->
    true;
is_alpha(_) ->
    false.

for other coding, you can add their rang of code and add them.

like image 79
Ahmad ElShareif Avatar answered Mar 16 '23 13:03

Ahmad ElShareif


There are three io_lib functions specifically for this:

  • io_lib:printable_list/1
  • io_lib:printable_latin1_list/1
  • io_lib:printable_unicode_list/1

Here is an example of one in use:

-spec show_message(ExParent, Message) -> ok
    when WxParent :: wx:wx_object(),
         Message  :: unicode:chardata() | term().

show_message(WxParent, Message) ->
    Format =
        case io_lib:printable_unicode_list(Message) of
            true  -> "~ts";
            false -> "~tp"
        end,
    Modal = wxMessageDialog:new(WxParent, io_lib:format(Format, [Message])),
    _ = wxMessageDialog:showModal(Modal),
    ok = wxMessageDialog:destroy(Modal).

Check out the io_lib docs: http://www.erlang.org/doc/man/io_lib.html#printable_list-1

Addendum

Because this subject isn't always easy to research in Erlang a related, but slightly broader Q/A might be of interest:

How to check whether input is a string in Erlang?

like image 33
zxq9 Avatar answered Mar 16 '23 14:03

zxq9