如何解决ESP32 正则表达式搜索所有迭代
我正在使用蓝牙并接收这样的 CSS 渐变字符串:
(90deg,rgba(66,68,90,1) 0%,rgba(72,79,164,1) 100%)
我正在使用正则表达式来拉出角度的“90”:
regex r_dir("([0-9]{2,3}deg)");
smatch m_dir;
regex_search(data,m_dir,r_dir);
string temp = m_dir[0].str();
int dir = atoi(temp.replace(temp.find("deg"),3,"").c_str());
这很好用,我可以得到“90”。我还需要提取颜色(及其百分比)。不幸的是,没有内置函数来返回字符串中的所有匹配项,所以我使用了我在网上找到的示例:
regex r_col("(rgba\\(([0-9]{1,3}(,|,|)){4}\\) [0-9]{1,3}%)");
sregex_iterator iter(data.begin(),data.end(),r_col);
sregex_iterator end;
while(iter != end)
{
Serial.println("fff");
for(unsigned i = 0; i < iter->size(); ++i)
{
Serial.println( (*iter)[i].str().c_str());
}
//Serial.println((*iter)[0].str().c_str());
++iter;
}
然而,这不仅不起作用,还会导致我的 ESP 崩溃:
Guru Meditation Error: Core 0 panic'ed (Unhandled debug exception)
Debug exception reason: Stack canary watchpoint triggered (Btc_task)
Core 0 register dump:
PC : 0x400d38af PS : 0x00060e36 A0 : 0x800d399d A1 : 0x3ffcd5f0
A2 : 0x3ffcf110 A3 : 0x00000001 A4 : 0x3ffe1218 A5 : 0x3ffe53f4
A6 : 0x3ffc5868 A7 : 0x3ffe0e5f A8 : 0x3ffe0e5e A9 : 0x3f401aa4
A10 : 0x3ffcf110 A11 : 0x00000001 A12 : 0x0000007f A13 : 0x3ffe5354
A14 : 0x3ffc5868 A15 : 0x00000000 SAR : 0x00000010 EXCCAUSE: 0x00000001
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff
Backtrace: 0x400d38af:0x3ffcd5f0 0x400d399a:0x3ffcd660 0x400d399a:0x3ffcd6d0 0x400d3b6d:0x3ffcd740 0x400d3856:0x3ffcd760 0x400d399a:0x3ffcd7d0 0x400d3b6d:0x3ffcd840 0x400d3856:0x3ffcd860 0x400d399a:0x3ffcd8d0 0x400d399a:0x3ffcd940 0x400d399a:0x3ffcd9b0 0x400d38cd:0x3ffcda20 0x400d38cd:0x3ffcda90 0x400d3ac1:0x3ffcdb00 0x400d3899:0x3ffcdb70 0x400d3ac1:0x3ffcdbe0 0x400d399a:0x3ffcdc50 0x400d3899:0x3ffcdcc0 0x400d38cd:0x3ffcdd30 0x400d38cd:0x3ffcdda0 0x400d399a:0x3ffcde10 0x400d399a:0x3ffcde80 0x400d3a69:0x3ffcdef0 0x400d3a69:0x3ffcdf60 0x400d3899:0x3ffcdfd0 0x400d399a:0x3ffce040 0x400d3b6d:0x3ffce0b0 0x400d3856:0x3ffce0d0 0x400d399a:0x3ffce140 0x400d3b6d:0x3ffce1b0 0x400d3856:0x3ffce1d0 0x400d399a:0x3ffce240 0x400d3899:0x3ffce2b0 0x400d38cd:0x3ffce320 0x400d38cd:0x3ffce390 0x400d399a:0x3ffce400 0x400d399a:0x3ffce470 0x400d3a69:0x3ffce4e0 0x400d3a69:0x3ffce550 0x400d3899:0x3ffce5c0 0x400d3ac1:0x3ffce630 0x400d399a:0x3ffce6a0 0x400d3b6d:0x3ffce710 0x400d3856:0x3ffce730 0x400d399a:0x3ffce7a0 0x400d3899:0x3ffce810 0x400d38cd:0x3ffce880 0x400d38cd:0x3ffce8f0 0x400d399a:0x3ffce960 0x400d399a:0x3ffce9d0 0x400d3a69:0x3ffcea40
Rebooting...
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0,SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO,clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:8896
load:0x40080400,len:5828
entry 0x400806ac
这是由 (*iter)[i]
引起的,虽然我不知道为什么。我在上面发现了 this GitHub 问题,但仅此而已。
我已经尝试过 Regexp
,特别是 iterating over a string 上的示例。该示例导致我的 ESP 崩溃,但是我在 SO 帖子中找到的这个示例不会导致崩溃,只是找不到匹配项:
ms.Target ((char *)data.c_str());
unsigned int count = ms.MatchCount ("(rgba\\(([0-9]{1,3}%)");
Serial.print ("Found ");
Serial.print (count);
Serial.println (" matches.");
for (int j = 0; j < count; j++)
{
Serial.print ("Capture number: ");
Serial.println (j,DEC);
Serial.print ("Text: '");
Serial.print (ms.GetCapture ((char *)data.c_str(),j));
Serial.println ("'");
}
输出为:
Found 0 matches.
有人知道匹配 ESP 上的所有事件吗?
解决方法
当使用原型板时,内存有限,而且 C 也仅限于核心依赖项。
我已经用 ESP32 启动了我的 NodeMcu 板来测试你的方法。
只需使用更简单的 substring() 方法即可直接从字符串中获取值。
不要使用正则表达式,而是使用以下 substring() - 一旦您的 css 不更改其关键属性,而只更改值-。然后您可以将结果存储在变量中并制作您的逻辑。
//rework this to accept collection,the datainit,and the
func fetchMachines2 (_ userID: String) {
var machines = [Machine]()
db.collection("machines").whereField("userID",isEqualTo: userID).getDocuments { (snapshot,error) in
if error == nil && snapshot != nil {
for doc in snapshot!.documents {
let mData = Machine(
imgURL: doc["imgURL"] as? String,machineID: doc["machineID"] as? String,name: doc["name"] as? String,type: doc["type"] as? String,userID: doc["userID"] as? String
)
machines.append(mData)
}
self.delegateMFP?.receiveMachines(machines: machines)
}
else {
print ("there is some error with the get documents")
}
}
}
试试看!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。