0

I have the following piece of code:

      static private void parseForSampleRate(string header, ref LoggerData logger_data)
      {
           Regex pattern = new Regex(@"(?<=Sample rate(hz): )\d+.\d+(?= |)");
           Match m = pattern.Match(header);

           if (m.Success)
                logger_data.SampleRate = Convert.ToDouble(m.Value);
           else
                logger_data.Error = ParseError.SAMPLE_RATE_NOT_FOUND;
      }

When feeding in the following string:

Sensor ID: 11 | Sample rate(hz): 60.00 | Duration(sec): 3600.00

I fail to receive a match.

What exactly am I doing wrong? I've specified a lookbehind of "Sample rate(hz): " and lookahead of " |". I've also believe I've specified the "\d+.\d+".

Any ideas as to why I'm not getting a success match with value of 60.00?

Thanks,

Izzo
  • 4,461
  • 13
  • 45
  • 82
  • 1
    `(hz)` -> `\(hz\)` – Wiktor Stribiżew Sep 20 '17 at 21:38
  • @WiktorStribiżew Hmmm... I only assumed I need those if I left out the @ at the front of the string. What exactly does the @ do in this scenario? – Izzo Sep 20 '17 at 21:40
  • If you declare the regex pattern with a regular string literal, you would need `\\(hz\\)`. `@` before `""` makes it a *verbatim* string literal that does not support string escape sequences. – Wiktor Stribiżew Sep 20 '17 at 21:41
  • Okay, that makes sense. "(" is a special character within the context of C# regex engine, but it is not a special character in context of C# string literals. That fixed my problem by the way, thank you. – Izzo Sep 20 '17 at 21:50

0 Answers0