[erlang-questions] parsing text
Michael McDaniel
erlangy@REDACTED
Thu Apr 29 21:00:09 CEST 2010
if I am understanding correctly what you want to do,
how about something like ...
X = <<"<B>Page Counter</B></TD><TD>4880</TD></TR>">> ,
{match, [Val]} = re:run(X, ".*</td><td>(?<VAL>.*)</td>.*", [caseless,{capture,['VAL'],list}]) ...
presuming your string is a constant format
~Michael
On Thu, Apr 29, 2010 at 12:39:38PM -0600, Wes James 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
>
> ________________________________________________________________
More information about the erlang-questions
mailing list