• Post Reply Bookmark Topic Watch Topic
  • New Topic

regexp Help!!  RSS feed

 
M Mistroni
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
i am trying to parse a fix protocol string using a regular expression, but somehow i am failing and i don't understand why. do bear in mind, i have never used regexp before and even though i read
many tutorials, there's still something i am missing.

Here's the string i am trying to parse. it's a Fix protocol string that starts always with 8=Fix and end always with 10=\\d{3} (3 digit string).

8=Fix4.2;49=TW;35=A;56=ISLD;34=1;10=140;8=Fix4.1;49=ZZ;35=D;56=IS;34=44;10=140;

As you can notice, there are 2 messages in the string above.
I am using this regexp to parse it

8=Fix.*\\D10=\\d{3};

But whenever i use that pattern, the 'match' found is the whole string, as effectively it starts with 8=Fix and ends with 10=140.

It should match the two substring, one that starts at 8=Fix4.2 and ends at 10=140 and the other that starts at 8=Fix4.1 and ends at 10=140

could anyone help ?

rgds
marco






i am guessing it's not the correct one... that pattern says match everything between 8=Fix and 10= followed by 3 digits.



 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this was my problem I'd use a String.split(";") followed by a String.split("=") of the items of the resulting String[]. Seems much easier than trying to cook up a regexp that does it in one step.
 
M Mistroni
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Lester..

thanks for the suggestion..

my current code uses split etc, and i have thought that regexp could help me doing it in a better way...

i'll stick to what i have then

thanks and regards
marco

 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just change the greedy match in the middle to a reluctant one -- change ".*" to ".*?".

As the greedy match, it will match as much as possible, from just after the start of the first message to just before the end of the last message. As a reluctant match, it will match as little as possible, which should stop it from eating the end tags of the first message.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!