3

The slere regex library does not work for regex from (? I) for case-insensitive...

 2 years ago
source link: https://www.codesd.com/item/the-slere-regex-library-does-not-work-for-regex-from-i-for-case-insensitive-search.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The slere regex library does not work for regex from (? I) for case-insensitive search

advertisements

I have a strange problem. I am looking for a light weight regex library to use with Visual Studio. Someone recommended me https://github.com/cesanta/slre; this library is all I need, and it is very light weight.

The problem is that it does not work under Visual Studio 2013. I compiled it with the following example but the printf statament is never reached.

static const char *str =
  "<img src=\"HTTPS://FOO.COM/x?b#c=tab1\"/> "
  "  <a href=\"http://cesanta.com\">some link</a>";

static const char *regex = "(?i)((https?://)[^\\s/'\"<>]+/?[^\\s'\"<>]*)";
struct slre_cap caps[2];
int i, j = 0, str_len = strlen(str);

while (j < str_len &&
       (i = slre_match(regex, str + j, str_len - j, caps, 2, 0)) > 0) {
  printf("Found URL: [%.*s]\n", caps[0].len, caps[0].ptr);
  j += i;
}

Could somebody take a look, or recommend me what I am doing wrong.


There seems to be a bug in the slre library with case insensitive matching by starting your regex with (?i). They have your example even in their unit tests but do not check if this test passes :)

Good news is that you can do case insensitive matching with a workaround by removing the (?i) part at the beginning of your regex and change the last argument in your call to slre_match to SLRE_IGNORE_CASE (or simply 1 as this is the value this constant is set to in slre.h) instead of plain 0.

Therefore the correct usage of slre_match in your example is

slre_match(regex, str + j, str_len - j, caps, 2, SLRE_IGNORE_CASE)

and remove the (?i) part at the beginning of your regex.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK