I found a header file in esp-idf under newlib but no examples or relevant code snippets online. Is this considered a working library or not? In case it doesn't, is there any alternative or lightweight alternative? Thanks.
Here is a link someone posted a while ago about regex in general:
http://www.cplusplus.com/reference/regex/
Hopefully I can include it but how?
#include <regex.h>
#include "newlib/include/regex.h"
Else? I'll start exploring and post my findings in case someone else reads this post in the future, looking for the same information.
Regular expression regex.h
Re: Regular expression regex.h
OK this include works:
Since I couldn't find examples of how to use it, or source code to read, I am hoping that someone can give me an example of matching.
Code: Select all
#include "regex.h"
Re: Regular expression regex.h
I hodge podged the few things I could find online. My code compiled but failed to link:
Error:
Code: Select all
#include <regex.h>
void app_main()
{
printf("Hello world!\n");
char a[] = "123456789012";
regex_t b;
b.re_magic=0;
int reti;
reti=regcomp(&b,"^(\\d{8}|\\d{12,14})$",0);
if (reti)
{
printf("Can't compile re\r\n");
}
else
{
reti = regexec(&b, a, 0, NULL, 0);
if (!reti)
{
printf("Match\r\n");
}
else if (reti==REG_NOMATCH)
{
printf("No Match\r\n");
}
}
regfree(&b);
}
LD build/hello-world.elf
D:/Downloads/msys32_iot/home/user/esp/Projects/hello_world/build/main\libmain.a(hello_world_main.o):(.literal.app_main+0x40): undefined reference to `regcomp'
D:/Downloads/msys32_iot/home/user/esp/Projects/hello_world/build/main\libmain.a(hello_world_main.o):(.literal.app_main+0x44): undefined reference to `regexec'
D:/Downloads/msys32_iot/home/user/esp/Projects/hello_world/build/main\libmain.a(hello_world_main.o):(.literal.app_main+0x48): undefined reference to `regfree'
D:/Downloads/msys32_iot/home/user/esp/Projects/hello_world/build/main\libmain.a(hello_world_main.o): In function `app_main':
D:/Downloads/msys32_iot/home/user/esp/Projects/hello_world/main/hello_world_main.c:24: undefined reference to `regcomp'
D:/Downloads/msys32_iot/home/user/esp/Projects/hello_world/main/hello_world_main.c:31: undefined reference to `regexec'
D:/Downloads/msys32_iot/home/user/esp/Projects/hello_world/main/hello_world_main.c:38: undefined reference to `regfree'
Re: Regular expression regex.h
We have a C++11 compiler so:
https://en.cppreference.com/w/cpp/regex
should work.
Not tried above but everything else C++11 I have tried works (and is portable)!
https://en.cppreference.com/w/cpp/regex
should work.
Not tried above but everything else C++11 I have tried works (and is portable)!
& I also believe that IDF CAN should be fixed.
Re: Regular expression regex.h
Hi zliudr,
Unfortunately the newlib we provide with ESP-IDF was compiled without regular expression support. The header is still there as it's part of newlib, but the symbols aren't actually available to link against.
We may be able to fix this in the future, if you have a GitHub account then you can subscribe to this Issue: https://github.com/espressif/esp-idf/issues/2407
Sorry for the inconvenience. As PeterR mentions, if using C++ then the std::regex implementation works.
Angus
Unfortunately the newlib we provide with ESP-IDF was compiled without regular expression support. The header is still there as it's part of newlib, but the symbols aren't actually available to link against.
We may be able to fix this in the future, if you have a GitHub account then you can subscribe to this Issue: https://github.com/espressif/esp-idf/issues/2407
Sorry for the inconvenience. As PeterR mentions, if using C++ then the std::regex implementation works.
Angus
Re: Regular expression regex.h
Thanks guys! Can I mix c stdio with c++ iostream? I do not use iostream. It would the worst thing that happens to c if iostream were to replace stdio.
Re: Regular expression regex.h
You can mix C++ with C.
The example uses iostream but you do not need to.
When the regex libray produces a std::string, just do this:
and if you are getting something akin to an ostringstream then:
but really, try the C++ way, easier in the long run!
PS
I regularly mix cout & printf when using legacy code. All is fine.
The example uses iostream but you do not need to.
When the regex libray produces a std::string, just do this:
Code: Select all
std::string result = ... something which generated a string
printf("%s\n", result.c_str());
and if you are getting something akin to an ostringstream then:
Code: Select all
printf("%s\n", myostringstream.str().c_str());
PS
I regularly mix cout & printf when using legacy code. All is fine.
& I also believe that IDF CAN should be fixed.
Re: Regular expression regex.h
Thanks PeterR! I'll give it a try. Although C++ style seems great to some, I see it as a big mess when I read the code and when I need complete control of the output width etc. Much better to have sprintf in my opinion to separate the stages of output preparation and actual sending output to stdout or file etc. I also prefer function calls than adding new syntax when a problem needs to be solved.
Who is online
Users browsing this forum: No registered users and 126 guests