• Post Reply Bookmark Topic Watch Topic
  • New Topic

parser ( strings based on delimiters ) and building list  RSS feed

 
Abagesh Kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have data which looks like this ( example strings giiven below)

I want to parse this string ( Notice that the delimiter can be | , a space or <> some other one)

And the substrings in each string have to be parsed and the data needs to be populated into an list which looks like this ( given below). The major problem is that the position of the substring differs depending on the string ( 3 types of examples are given below ) Means Eg: say UnitNo in string 1 is in position 19 and in String 2 is in Pos 1. L1 is in Positin 3 in String 1 and L1 is in position 6 in string 2 .

How to write a parser which will parse based on the delimiter, and the beginning of string ( string1 starts with ^, string 3 starts with ^id ) and build and populate the list extracting appropriate strings

Any ideas will be helpful .


Thnanks


-----List---- UnitNo L1 L2 STime speed direction status preading greading freading lreading Ireading PSource Battery1 Battery2 ID i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 o1 o2 o3 o4 o5 o6 o7 o8 o9 o10 o11 o12 a1 a2 a3 a4 a5


--------------------------------------------------------------------------------

String 1

^0609|8|2829.49673|07704.82322|13.627|130.59|140312075012|2|0|A|1|0|0|0|3.015600|12.230000|2748.26|76373.00|352024027728165|!

String 2

FC03 00140700 00007D04 0002E702 0001624F 00C02A9D 049C5D4C 00000014 00025A39 00000008 000FE348

String 3 ^id=PTTRK30ES861001000245502&POS=<3.1><1259.429640><07735.382010><084038><260312><0.000><0.0><22><06><0><0><67><0><0><0.09><0.0><5.73><2.2><0><0.0>@

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks to me like you should have three different parsers instead of just one. Then use some logic to detect which one to use. For example, look at the first few characters:

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you got a grammar for your Strings? You can’t sensibly parse anything until you have a grammar.
 
Abagesh Kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.


All the fields I have suggested in this list are present in the strings. The delimiter is different. The starting structure is different and MOST importantly the position in which they appear is different.

UnitNo L1 L2 STime speed direction status preading greading freading lreading Ireading PSource Battery1 Battery2 ID i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 o1 o2 o3 o4 o5 o6 o7 o8 o9 o10 o11 o12 a1 a2 a3 a4 a5




Thanks
 
Abagesh Kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is it possible to do the following in java :

Have formats defined for Strings ( as shown below)


Can we map a string to a particular format with this delimiter and build a "key/value" list ( which can be dumped into db. Key matches column name)

I dont know if this is a good idea and since i am new to java, I dont know how this can be done in java . Any ideas will be of great help.

Thanks
===
String 1
Format1
UnitNo L1 L2 STime speed direction status preading greading freading lreading Ireading PSource Battery1 Battery2 ID i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 o1 o2 o3 o4 o5 o6 o7 o8 o9 o10 o11 o12 a1 a2 a3 a4 a5


^ID|Uid|L1|L2|Stime|Speed|Direction|Status|preading|greading|Ireading1|Ireading2|Psource|Battery1|Battery2|I1|I2|I3|UnitNo.............
^0609|8|2829.49673|07704.82322|13.627|130.59|140312075012|2|0|A|1|0|0|0|3.015600|12.230000|2748.26|76373.00|352024027728165|!

String 2
Format2
UnitNo L1 L2 Stime Speed Direction Status I1 I2 I3 Preading Ireading Battery1 preading1 greading1.............
FC03 00140700 00007D04 0002E702 0001624F 00C02A9D 049C5D4C 00000014 00025A39 00000008 000FE348

Format3
String 3
^id=UnitNo&Pos=<I1><I2><Stime><Speed><Direction>.......................
^id=PTTRK30ES861001000245502&POS=<3.1><1259.429640><07735.382010><084038><260312><0.000><0.0><22><06><0><0><67><0><0><0.09><0.0><5.73><2.2><0><0.0>@



================================
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!