nicolaus-hee
nicolaus-hee

Reputation: 789

Regular expression: No results even though confirmed in regex tester

I want to isolate HDMI from the string >*sour=?#*SOUR=HDMI# (HDMI varies and can have different lengths). I came up with this regular expression to do that: (?<=SOUR=)[^#]*. Using this online regex tester, I confirmed that this is in fact working.

enter image description here

In my Arduino code, I'm using Nick Gammon's RegExp library. I followed Nick's documentation and this code runs fine and finds two matches, just like in his example. However, when I insert my above Target and Match parameters, the Match method returns no matches (i.e. Serial.println("no match") is executed). Any advice?

Here's my code:

#include <Regexp.h>

void setup ()
{
  Serial.begin(115200);
  MatchState ms;
  ms.Target (">*sour=?#*SOUR=HDMI#");

  char result = ms.Match ("(?<=SOUR=)[^#]*");
  if (result == REGEXP_MATCHED)
  {
    char buf [100]  
    Serial.print ("Captures: ");
    Serial.println (ms.level);

    for (int j = 0; j < ms.level; j++)
      {
      Serial.print ("Capture number: ");
      Serial.println (j, DEC);
      Serial.print ("Text: '");
      Serial.print (ms.GetCapture (buf, j));
      Serial.println ("'");
      }
  }
  else if (result == REGEXP_NOMATCH)
  {
    Serial.println("no match");
  }
}

void loop () {}

Upvotes: 1

Views: 1815

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627145

The library you are using does not suppport lookbehinds.

You may solve the problem using a capturing group:

 char result = ms.Match ("SOUR=([^#]*)");

Upvotes: 1

Related Questions