[erlang-questions] parsing text

Colm Dougan colm.dougan@REDACTED
Fri Apr 30 21:46:26 CEST 2010


If you already found the offset of the start of the number then you
can use string:to_inetger (or more generally io_lib:fread) to extract
the number, regardless of it being variable length.

Eshell V5.7.5  (abort with ^G)
1> string:to_integer("4880</TD></TR>").
{4880,"</TD></TR>"}

Eshell V5.7.5  (abort with ^G)
1> io_lib:fread("<TD>~d</TD></TR>", "<TD>4880</TD></TR>").
{ok,[4880],[]}

Colm


On Thu, Apr 29, 2010 at 11:39 AM, Wes James <comptekki@REDACTED> wrote:
> I have a function grabbing a page and I'm pulling text out of the
> result.  I can get the line:
>
> lists:nth(424,B).
> <<"<B>Page Counter</B></TD><TD>4880</TD></TR>">>
>
>
> but 4880 will eventually get to 10000, etc.
>
> I can do
>
> string:substr(binary_to_list(lists:nth(424,B)), 34, 4).
>
> to get the 4880 out, but to do a check for > 9999 would I grab
> position 34 + the rest of the text then test positions after that for
> int?
>
> or maybe something like this?
>
>
> A=string:substr(binary_to_list(lists:nth(424,B)), 34, 6)
> B=string:substr(binary_to_list(lists:nth(424,B)), 34, 5)
> C=string:substr(binary_to_list(lists:nth(424,B)), 34, 4)
> case
>    is_integer(A) -> A;
>    is_integer(B) -> B;
>    is_integer(C) -> C;
> end.
>
>
> Hmm - A, B, and C will be strings though so is_integer won't work.  If
> I do list_to_integer(A), I guess I can write something like this:
>
> try list_to_integer(A)
> catch
>    throw:Term -> Term;
>    exit:Reason -> {'EXIT',Reason};
>    error:Reason -> {'EXIT',{Reason,erlang:get_stacktrace()}}
> end.
>
>
> and so forth....
>
> -wes
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>


More information about the erlang-questions mailing list