help needed with regular expression


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


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
f) an A or a V
g) D
h) 6 digits
i) T
j) 6 digits
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


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
Who is Participating?
Todd MummertConnect With a Mentor Commented:

for most of your decimals you may want to use:

(\d+(?:\.\d+)?)       # this will match simple number (1, 12, etc), as well as ones that include a decimal followed by more digits

You can add the additional specifications \d{min,max}   rather than using \d if you want

for the not always preset ,*, you can use


the same for the not always present ALARM



will match a numeric sequence, a decimal, and another numeric sequence.

Do you need to specify the length maximums?
andiejeAuthor Commented:
climbgunks, please could you explain how this matches a decimal?


Why are there 2 lots of brackets? I also don't know what the ?:\ bit means

However that is more detailed than I need because the numbers are always expressed as a decimal (i think the above accounts for optional digits after an optional decimal place)
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

andiejeAuthor Commented:
regarding this:


I see this means

\d+  one of more digits

followed by optional decimal place \. and one or more digits again \d+

This would give (\d+(\.\d+)?) to me. I don;t know what the ?: is for
Todd MummertCommented:
the ?: after the opening (  tells perl not to add it to the fields... just makes things neater

usually every pair of ()'s would count as a field   $1, $2, etc

so we don't want a field with just the  stuff after the decimal point (and the decimal point)... the entire number is already included in the previous cluster

sorry for the confusion
andiejeAuthor Commented:
Im trying to do it bit by bit and I can't even get this first bit to work


can you see anything wrong with that?

andiejeAuthor Commented:
i think $ might mean end of line?
Todd MummertCommented:
escape the $
andiejeAuthor Commented:
More problems

This works

           Dim gpsRegExString As String = "\$ID,(\d{15}),(ALARM,0x00000010,)?IOP,[I|O],([0x00|0x10]),.*"

but when i add GPSEX to the end it stops matching

  Dim gpsRegExString As String = "\$ID,(\d{15}),(ALARM,0x00000010,)?IOP,[I|O],([0x00|0x10]),GPSEX.*"


andiejeAuthor Commented:
My mistake, it stops working when i add the comma

so this works
but this does not  "\$ID,(\d{15}),(ALARM,0x00000010,)?IOP,[I|O],([0x00|0x10]),.*"
Todd MummertCommented:

your use of [] is incorrect...

it's not used to delimit choices, but to create matching character sets...

so you want

(I|O)   and (0x00|0x10)    

this [0x00|0x10]   matches a single character from  [0x|1]     including the character '|'  which isn't what you want at all.

andiejeAuthor Commented:
thanks for your help, all working
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.