Regular expression to grab unknown value

  • Thread starter Thread starter Eric Cathell
  • Start date Start date
E

Eric Cathell

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=;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
 
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, string> result = 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;
string value = result.Groups["value"].Captures;
}
}

Jesse

Hello Eric,
I think i found the answer

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

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
 
Back
Top