469,270 Members | 1,011 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Regular expression to grab unknown value

I have a string of data coming from a device. I have to pull the data out of
the string that I need to put into an object.

this is an example of my return string:

each property in my object matches a return value(ie there is a property
called ecgSt1)

so I really need to know how to pull out the value between the = and the ;

ecgSt1=-65;ecgSt2=+65;ecgSt3=-45;p1=21,45,54;p2=45,55,21;t1=99;hr=45;hrSource=;n ibp=;nibpElapsedTime=;resp=;respSource=;co2=;spo2= ;o2=;agent=;agentType=;n2o=
x.ecgst1=regex.Something(pattern,string)etc... this should pull the value
between the = and the ;
I have searched pretty thouroughly on the web, but I cant seem to get the
right search terms going.

Eric
Nov 14 '08 #1
2 2189
I think i found the answer

ecgSt1=\s*([^;\s]*)
"Eric Cathell" <de**********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I have a string of data coming from a device. I have to pull the data out
of the string that I need to put into an object.

this is an example of my return string:

each property in my object matches a return value(ie there is a property
called ecgSt1)

so I really need to know how to pull out the value between the = and the ;

ecgSt1=-65;ecgSt2=+65;ecgSt3=-45;p1=21,45,54;p2=45,55,21;t1=99;hr=45;hrSource=;n ibp=;nibpElapsedTime=;resp=;respSource=;co2=;spo2= ;o2=;agent=;agentType=;n2o=
x.ecgst1=regex.Something(pattern,string)etc... this should pull the value
between the = and the ;
I have searched pretty thouroughly on the web, but I cant seem to get the
right search terms going.

Eric

Nov 14 '08 #2
using the following regex you'd get a MatchCollection with app possible key/value
pairs which can be added to a SDictionary quite easily. You'd be on your
own from there:

(?<key>[^=]+)=(?<value>[^;]+);

regex rx = new Regex("(?<key>[^=]+)=(?<value>[^;]+);", RegexOption.None);

Dictionary<string, stringresult = new Dictionary<string, string>();

Match m = rx.Match(yourString);
while (m.success)
{
result.add=(m.Groups["key"].Value, m.Groups["value"].Value);
}
//Result now has all keys and values in an easily accessible collection.

Alternatively you could use:

(?:(?<key>[^=]+)=(?<value>[^;]+);)*

regex rx = new Regex("(?:(?<key>[^=]+)=(?<value>[^;]+);)*", RegexOption.None);

Match m = rx.Match(yourString);
if (m.success)
{
foreach(int i = 0; i < result.Groups["key"].Captures.Count; i++)
{
string key = result.Groups["key"].Captures[i];
string value = result.Groups["value"].Captures[i];
}
}

Jesse

Hello Eric,
I think i found the answer

ecgSt1=\s*([^;\s]*)

"Eric Cathell" <de**********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I have a string of data coming from a device. I have to pull the data
out of the string that I need to put into an object.

this is an example of my return string:

each property in my object matches a return value(ie there is a
property called ecgSt1)

so I really need to know how to pull out the value between the = and
the ;

ecgSt1=-65;ecgSt2=+65;ecgSt3=-45;p1=21,45,54;p2=45,55,21;t1=99;hr=45;
hrSource=;nibp=;nibpElapsedTime=;resp=;respSource =;co2=;spo2=;o2=;age
nt=;agentType=;n2o=

x.ecgst1=regex.Something(pattern,string)etc... this should pull the
value between the = and the ;

I have searched pretty thouroughly on the web, but I cant seem to get
the right search terms going.

Eric
--
Jesse Houwing
jesse.houwing at sogeti.nl
Nov 16 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Dave Miller | last post: by
3 posts views Thread by Ori | last post: by
4 posts views Thread by Neri | last post: by
2 posts views Thread by news.microsoft.com | last post: by
25 posts views Thread by Mike | last post: by
2 posts views Thread by Mr.Steskal | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.