Regular expression to grab unknown value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Cathell

    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;e cgSt3=-45;p1=21,45,54; p2=45,55,21;t1= 99;hr=45;hrSour ce=;nibp=;nibpE lapsedTime=;res p=;respSource=; co2=;spo2=;o2=; agent=;agentTyp e=;n2o=


    x.ecgst1=regex. Something(patte rn,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


  • Eric Cathell

    #2
    Re: Regular expression to grab unknown value

    I think i found the answer

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


    "Eric Cathell" <depictureboy@c ommunity.nospam wrote in message
    news:%23khFwIoR JHA.3880@TK2MSF TNGP04.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;e cgSt3=-45;p1=21,45,54; p2=45,55,21;t1= 99;hr=45;hrSour ce=;nibp=;nibpE lapsedTime=;res p=;respSource=; co2=;spo2=;o2=; agent=;agentTyp e=;n2o=
    >
    >
    x.ecgst1=regex. Something(patte rn,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
    >

    Comment

    • Jesse Houwing

      #3
      Re: Regular expression to grab unknown value

      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.Non e);

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

      Match m = rx.Match(yourSt ring);
      while (m.success)
      {
      result.add=(m.G roups["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("(?:(?<ke y>[^=]+)=(?<value>[^;]+);)*", RegexOption.Non e);

      Match m = rx.Match(yourSt ring);
      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" <depictureboy@c ommunity.nospam wrote in message
      news:%23khFwIoR JHA.3880@TK2MSF TNGP04.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;e cgSt3=-45;p1=21,45,54; p2=45,55,21;t1= 99;hr=45;
      >hrSource=;nibp =;nibpElapsedTi me=;resp=;respS ource=;co2=;spo 2=;o2=;age
      >nt=;agentType= ;n2o=
      >>
      >x.ecgst1=regex .Something(patt ern,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


      Comment

      Working...