ESP32无法根据Regexp掩码从字符串中提取字符串



已经做了大量的研究,但无法找到合适的格式的Regexp掩码,以便从另一个字符串提取一个字符串。

假设我有以下字符串:敏捷的棕色狐狸ABC3D97跳过了懒惰的狼我需要提取" abc3d97 &;基于掩码:/[A-Z]{3}d{1}[A-Z]{1}d{2}/但我就是找不到合适的语法,因为上面的语法和它的变化返回不匹配。

我的测试代码如下:
#include <Regexp.h>
void setup ()    {
Serial.begin (115200);
// match state object
MatchState ms;
// what we are searching (the target)
char buf [100] = "The quick brown fox ABC3D97 jumps over the lazy wolf";
ms.Target (buf);  // set its address
Serial.println (buf);
char result = ms.Match ("d{3}");   <-- returns no match. 

if (result > 0)    {
Serial.print ("Found match at: ");
int matchStart = ms.MatchStart;
int matchLength = ms.MatchLength;
Serial.println (matchStart);        // 16 in this case     
Serial.print ("Match length: ");
Serial.println (matchLength);       // 3 in this case
String text = String(buf);
Serial.println(text.substring(matchStart,matchStart+matchLength));
}
else
Serial.println ("No match.");

}  // end of setup  
void loop () {}

援助的欢迎。

您正在使用的库似乎是Nick Gammon从LUA移植的正则表达式功能。

LUA的正则表达式使用与其他常用正则表达式不同的语法。该库的README提供了到LUA正则表达式文档的链接。

LUA使用%而不是作为字符类,因此d需要写成%d。这个库也不支持{number}语法来指定匹配的数量。您必须重复匹配字符。

根据文档,匹配字符串应该是:

[A-Z][A-Z][A-Z]%d[A-Z]%d%d

[A-Z]{3}d{1}[A-Z]{1}d{2}