[erlang-questions] strip end off of string

Colm Dougan colm.dougan@REDACTED
Tue May 4 22:04:48 CEST 2010


On Tue, May 4, 2010 at 9:54 AM, Wes James <comptekki@REDACTED> wrote:
> I see that fist ++ rest below will match the first part of the string
> and the put the rest in "Rest":
>
> -module(t).
>
> -export([gett/0]).
>
> gett() -> t("<INPUT TYPE=\"hidden\" NAME=\"xyz\" VALUE=\"some_string\">").
>
> t("<INPUT TYPE=\"hidden\" NAME=\"xyz\" VALUE=\"" ++ Rest) ->
>        t2(Rest).
>
> t2(First ++ "\">") ->
>      io:format("~s~n", [First]).
>
>
> But in t2 is there something like that?

Bearing in mind that:

   extract_foo("Foo:" ++ Rest)

is just syntax sugar for :

   extract_foo([$F, $o, $o, $: | Rest])

You could write:

t("<INPUT TYPE=\"hidden\" NAME=\"xyz\" VALUE=\"" ++ Rest) ->
    t2(lists:reverse(Rest)).

t2([$>, $" | Prefix]) ->
    lists:reverse(Prefix).

(not necessarily recommended but I'm mentioning it for the purposes of
illustration).

> I tried some experiments with some_string\">:
>
> G="some_string\">".
> "some_string\">"
> 110> re:run("(?<VAL>.*)\">", [caseless,{capture,['VAL'],list}]).
> ** exception error: bad argument
>     in function  re:run/2
>        called as re:run("(?<VAL>.*)\">",[caseless,{capture,['VAL'],list}])

How about:

Eshell V5.7.5  (abort with ^G)
1> Str = "<INPUT TYPE=\"hidden\" NAME=\"xyz\" VALUE=\"some_string\">".
"<INPUT TYPE=\"hidden\" NAME=\"xyz\" VALUE=\"some_string\">"
2> re:run(Str, "VALUE=\"(\\w+)", [{capture,[1],list}]).
{match,["some_string"]}


Colm


More information about the erlang-questions mailing list