Regular expression regex.h

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

Regular expression regex.h

Postby zliudr » Tue Oct 22, 2019 6:21 pm

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.

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

Re: Regular expression regex.h

Postby zliudr » Wed Oct 23, 2019 2:34 am

OK this include works:

Code: Select all

#include "regex.h"
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.

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

Re: Regular expression regex.h

Postby zliudr » Wed Oct 23, 2019 3:47 am

I hodge podged the few things I could find online. My code compiled but failed to link:

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);
}
Error:
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'

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Regular expression regex.h

Postby PeterR » Mon Oct 28, 2019 7:27 pm

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)!
& I also believe that IDF CAN should be fixed.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Regular expression regex.h

Postby ESP_Angus » Mon Oct 28, 2019 10:52 pm

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

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

Re: Regular expression regex.h

Postby zliudr » Tue Oct 29, 2019 12:47 pm

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.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Regular expression regex.h

Postby PeterR » Thu Oct 31, 2019 4:28 pm

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:

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());
but really, try the C++ way, easier in the long run!

PS
I regularly mix cout & printf when using legacy code. All is fine.
& I also believe that IDF CAN should be fixed.

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

Re: Regular expression regex.h

Postby zliudr » Fri Nov 01, 2019 2:25 pm

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: Bing [Bot] and 83 guests