已经进行了大量研究,但无法找到正则表达式掩码的正确格式,以便从另一个字符串中提取字符串。
假设我有以下字符串:“The quick brown fox ABC3D97 jumps over the lazy wolf”我需要根据掩码提取“ABC3D97”:/[AZ]{3}\d{1}[AZ]{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 () {}
欢迎协助。