| Author |
Message |
< Erlang ~ String parser |
| phicarre |
Posted: Thu Mar 26, 2009 3:38 pm |
|
|
|
User
Joined: 23 Mar 2009
Posts: 14
|
I am looking for a string parser in erlang. Any idea ?
My string will be a list of fields separated by a comma. |
|
|
| Back to top |
|
| uwiger |
Posted: Fri Mar 27, 2009 1:22 pm |
|
|
|
User
Joined: 03 Jul 2006
Posts: 604
Location: Sweden
|
phicarre wrote: I am looking for a string parser in erlang. Any idea ?
My string will be a list of fields separated by a comma.
A very simple way to parse something like this is
Code: Lines = string:tokens(Str, [$\n]),
[string:tokens(L, [$,]) || L <- Lines]
...assuming that the string consists of lines of comma-separated values. Otherwise, skip the first step.
Erlang (BEAM) emulator version 5.6.5 [smp:2] [async-threads:0]
Code: Eshell V5.6.5 (abort with ^G)
1> Str = "15,16,17".
"15,16,17"
2> string:tokens(Str,[$,]).
["15","16","17"]
The call can also be written string:tokens(Str,","). |
|
|
| Back to top |
|
| phicarre |
Posted: Fri Mar 27, 2009 4:40 pm |
|
|
|
User
Joined: 23 Mar 2009
Posts: 14
|
Ok, but I would like to extract each token by supposing
[field1,field2,field3] then
field1 = 15
field2 = 16
field3 = 17 |
|
|
| Back to top |
|
| phicarre |
Posted: Fri Mar 27, 2009 6:55 pm |
|
|
|
User
Joined: 23 Mar 2009
Posts: 14
|
I read a little bit the doc and it suggests to represent a string as a bit stream because of the memory.
Then my question must be changed:
I have a bit stream and I want to extract from it fields separated by a comma.
S="hello,how,are,you".
How to interpret it as <<field1,field2,field3>> and after how to use each field.
if field1=hello ... |
|
|
| Back to top |
|
| prof3ta |
Posted: Mon Apr 20, 2009 12:55 pm |
|
|
|
User
Joined: 06 Feb 2009
Posts: 34
|
Something like this?
Code:
-module(parser).
-export([parse/1]).
parse(String) when is_list(String) ->
Binary = list_to_binary(String),
parse(Binary, [], []);
parse(Binary) when is_binary(Binary) ->
parse(Binary, [], []).
parse(<<>>, Acc, Tokens) ->
lists:reverse([lists:reverse(Acc)|Tokens]);
parse(<<$,,Rest/binary>> = Binary, Acc, Tokens) when is_binary(Binary) ->
parse(Rest, [], [lists:reverse(Acc)|Tokens]);
parse(<<Head, Rest/binary>> = Binary, Acc, Tokens) when is_binary(Binary) ->
parse(Rest, [Head|Acc], Tokens).
This code will consider empty terms as well, so that will return:
from:
Code: parser:parse("s,,t").
And:
from:
|
Last edited by prof3ta on Mon Apr 20, 2009 1:02 pm; edited 1 time in total |
|
| Back to top |
|
| prof3ta |
Posted: Mon Apr 20, 2009 1:00 pm |
|
|
|
User
Joined: 06 Feb 2009
Posts: 34
|
By the way, if you already know the "meaning and the size of the fields" that you have in your binary (for example, if you're parsing a well known protocol), you can always do something like:
Code:
case Packet of
<<Id:8, Name:256, Surname:256, Rest/binary>> ->
...
In this case you will have your values bounded to your variables, ready to use. |
|
|
| Back to top |
|
| wuji |
Posted: Sat Aug 11, 2012 2:15 am |
|
|
|
User
Joined: 10 Aug 2012
Posts: 654
|
than half a century, and a football team's worth of of Cheap Ralph Lauren Shirts of children and grandchildren."He died as he lived. He
hard until the end, stayed positive, thought only of others others replica Christian Louboutin others and constantly reminded everyone of how blessed his life
been. His ambitions were far reaching, but he never never discount designer *beep* never believed he had to leave this Happy Valley to
them. He was a man devoted to his family, family, [h4]designer replica *beep*[/h4] family, his university, his players and his community," Paterno's family
in a statement.While at Penn State's helm, Paterno, who was was [h2]replica designer bags for sale[/h2] was born in Brooklyn, N.Y., led the Nittany Lions to
undefeated seasons and two NCAA championships, had only five losing losing cheap Ralph Lauren Polo losing seasons, was inducted into the College Football Hall of |
|
|
| Back to top |
|
| dongdongwu |
Posted: Thu Sep 20, 2012 5:34 am |
|
|
|
User
Joined: 19 Sep 2012
Posts: 236
|
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|