Is it possible to get all occurrences of \< in a string using regexec?

Arkadiusz Drabczyk arkadiusz@drabczyk.org
Mon Feb 10 21:20:06 GMT 2025


On Mon, Feb 10, 2025 at 07:52:46PM +0100, Florian Weimer wrote:
> This:
> 
>   size_t eo = strlen(text);
>   while (offset < len) {
>     match[0].rm_so = offset;
>     match[0].rm_eo = eo;
>     ret = regexec(&regex, text, 1, match, REG_STARTEND);
> 
>     if (ret == REG_NOMATCH) {
>       break;
>     }
> 
>     printf("match[0].rm_so == %d\n", match[0].rm_so);
>     printf("match[0].rm_eo == %d\n", match[0].rm_eo);
> 
>     int start = match[0].rm_so;
>     int end = match[0].rm_eo;
> 
>     printf("Match found at: %d to %d\n", start, end);
> 
>     offset = start + 1;
>   }
> 
> Gives me with glibc:
> 
> match[0].rm_so == 0
> match[0].rm_eo == 0
> Match found at: 0 to 0
> match[0].rm_so == 4
> match[0].rm_eo == 4
> Match found at: 4 to 4
> match[0].rm_so == 8
> match[0].rm_eo == 8
> Match found at: 8 to 8
> 
> Is this what you had in mind?  We should perhaps add it as an example to
> the manual.

ah, yes, thanks. I didn't set match[0].rm_so and match[0].rm_eo before
running regexec() with REG_STARTEND. So the conclusion is that if you
want to use GNU regular expression extensions you also have to use
non-POSIX flags in some cases.

-- 
Arkadiusz Drabczyk <arkadiusz@drabczyk.org>


More information about the Libc-help mailing list