Solved
help needed with regular expression
Posted on 2009-05-18
Hi
I have written a regular expression for a complication input string and I would like help to optimise or improve it please.
Here are some examples of the input string
556:$ID,354879020144852,IOP,I,0x10,GPSEX,A,D,060509,T,140521,La,52.97084,N,Lo,2.18178,W,V,0.3,*
556:$ID,354879020144852,ALARM,0x00000010,IOP,I,0x00,GPSEX,A,D,060509,T,140626,La,52.97082,N,Lo,2.18182,W,V,0.8
The format is explained below. I am assuming you can see the parts of teh string are delineated by commas
a) 3 or 4 digits followed by $ID:
b) variable 15 digits
c - not always present) ALARM,0x00000010
c) IOP followed by an I or an O
d)0x00 or 0x00
e) GPSEX
f) an A or a V
g) D
h) 6 digits
i) T
j) 6 digits
k)La
l) 0.00000 or 2 digits, decimal point, 5 digits e.g. 52.12345
m) N, lo
o) 0.00000 or 1 digit, decimal point, 5 digits e.g. 2.12345
p) W or E
q) V
r) 0.0 or 1 to 3 digits, decimal point, 2 digits e.g 0.3, 45.3, 101.3
s - not always present) ,*
Here is what i have so far
\d{3,4}$ID,(\d{15}),IOP,[I|O],([0x00|0x10]),GPSEX,([A|V]),D,(\d{6}),T,(\d{6}),La,(.{7,8}),N,Lo,.{7},([W|E]),V,
I am particularly stuck on the last bit. The number that comes after the V is sometimes followed by a comma and a *
I need to save the number that follows the V but i don't know how to. How do i write a character set that says "character can be number from 0 to 9 or a decimal point only"
Many thanks
andrea