regex.3: Add example program
PATCH 1/1 regex.3: Add example program
$ gcc -Wall -Wextra -Werror -pedantic regex.c -o regex $ ./regex String = "1) John Driverhacker; 2) John Doe; 3) John Foo; " #0 match: rm_so = 25; rm_eo = 32; len = 7 substring = "John Do" #1 match: rm_so = 6; rm_eo = 14; len = 8 substring = "John Foo"
---
Hello Michael,
Here's the example for regex.3 :-}
Best regards,
Alex
man3/regex.3 | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
man3/regex.3 +44 -0
@@ -337,6 +337,50 @@ T} Thread safety MT-Safe .TE .SH CONFORMING TO POSIX.1-2001, POSIX.1-2008. +.SH EXAMPLES +.EX +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <regex.h> + +#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0])) + +static const char *const str = + "1) John Driverhacker;\en2) John Doe;\en3) John Foo;\en"; +static const char *const re = "John.*o"; + +int main(void) +{ + static const char *s = str; + regex_t regex; + regmatch_t pmatch[1]; + regoff_t len; + + if (regcomp(®ex, re, REG_NEWLINE)) + exit(EXIT_FAILURE); + + printf("String = \\"%s\\"\en", str); + + for (ptrdiff_t i = 0; ; i++) { + if (regexec(®ex, s, ARRAY_SIZE(pmatch), pmatch, 0)) + break; + + len = pmatch[0].rm_eo \- pmatch[0].rm_so; + printf("#%td match:\en", i); + printf("rm_so = %jd; rm_eo = %jd; len = %jd\en", + (intmax_t) pmatch[0].rm_so, + (intmax_t) pmatch[0].rm_eo, + (intmax_t) len); + printf("substring = \\"%.*s\\"\en", len, s + pmatch[0].rm_so); + + s += pmatch[0].rm_eo; + } + + exit(EXIT_SUCCESS); +} +.EE .SH SEE ALSO .BR grep (1), .BR regex (7)
Signature · 1 line
2.28.0
I realized now that the output is a bit weird:
I'm printing the offset incorrectly (except for the first match). I'll fix it in a few minutes.
Thanks,
Alex
Quoted 84 lines · Alejandro Colomar
$ gcc -Wall -Wextra -Werror -pedantic regex.c -o regex $ ./regex String = "1) John Driverhacker; 2) John Doe; 3) John Foo; " #0 match: rm_so = 25; rm_eo = 32; len = 7 substring = "John Do" #1 match: rm_so = 6; rm_eo = 14; len = 8 substring = "John Foo"Signed-off-by Alejandro Colomar <[email protected]>---
Hello Michael,
Here's the example for regex.3 :-}
Best regards,
Alex
man3/regex.3 | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)man3/regex.3 +44
-0@@ -337,6 +337,50 @@ T} Thread safety MT-Safe .TE .SH CONFORMING TO POSIX.1-2001, POSIX.1-2008. +.SH EXAMPLES +.EX +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <regex.h> + +#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0])) + +static const char *const str = + "1) John Driverhacker;\en2) John Doe;\en3) John Foo;\en"; +static const char *const re = "John.*o"; + +int main(void) +{ + static const char *s = str; + regex_t regex; + regmatch_t pmatch[1]; + regoff_t len; + + if (regcomp(®ex, re, REG_NEWLINE)) + exit(EXIT_FAILURE); + + printf("String = \\"%s\\"\en", str); + + for (ptrdiff_t i = 0; ; i++) { + if (regexec(®ex, s, ARRAY_SIZE(pmatch), pmatch, 0)) + break; + + len = pmatch[0].rm_eo \- pmatch[0].rm_so; + printf("#%td match:\en", i); + printf("rm_so = %jd; rm_eo = %jd; len = %jd\en", + (intmax_t) pmatch[0].rm_so, + (intmax_t) pmatch[0].rm_eo, + (intmax_t) len); + printf("substring = \\"%.*s\\"\en", len, s + pmatch[0].rm_so); + + s += pmatch[0].rm_eo; + } + + exit(EXIT_SUCCESS); +} +.EE .SH SEE ALSO .BR grep (1), .BR regex (7)
PATCH v2 1/1 regex.3: Add example program
$ gcc -Wall -Wextra -Werror -pedantic regex.c -o regex $ ./regex.3 String = "1) John Driverhacker; 2) John Doe; 3) John Foo; " Matches: #0: offset = 25; length = 7 substring = "John Do" #1: offset = 38; length = 8 substring = "John Foo"
---
Hi Michael,
Now it's much better :)
Cheers,
Alex
man3/regex.3 | 42 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
man3/regex.3 +42 -0
@@ -337,6 +337,48 @@ T} Thread safety MT-Safe .TE .SH CONFORMING TO POSIX.1-2001, POSIX.1-2008. +.SH EXAMPLES +.EX +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <regex.h> + +#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0])) + +static const char *const str = + "1) John Driverhacker;\en2) John Doe;\en3) John Foo;\en"; +static const char *const re = "John.*o"; + +int main(void) +{ + static const char *s = str; + regex_t regex; + regmatch_t pmatch[1]; + regoff_t off, len; + + if (regcomp(®ex, re, REG_NEWLINE)) + exit(EXIT_FAILURE); + + printf("String = \\"%s\\"\en", str); + printf("Matches:\en"); + + for (int i = 0; ; i++) { + if (regexec(®ex, s, ARRAY_SIZE(pmatch), pmatch, 0)) + break; + + off = pmatch[0].rm_so + (s \- str); + len = pmatch[0].rm_eo \- pmatch[0].rm_so; + printf("#%d:\en", i); + printf("offset = %jd; length = %jd\en", (intmax_t) off, (intmax_t) len); + printf("substring = \\"%.*s\\"\en", len, s + pmatch[0].rm_so); + + s += pmatch[0].rm_eo; + } + + exit(EXIT_SUCCESS); +} +.EE .SH SEE ALSO .BR grep (1), .BR regex (7)
Signature · 1 line
2.28.0
Oops, I should've used \e instead of \\ (see below). Please fix that :)
Thanks,
Alex
Quoted 50 lines · Alejandro Colomar
$ gcc -Wall -Wextra -Werror -pedantic regex.c -o regex $ ./regex.3 String = "1) John Driverhacker; 2) John Doe; 3) John Foo; " Matches: #0: offset = 25; length = 7 substring = "John Do" #1: offset = 38; length = 8 substring = "John Foo"Signed-off-by Alejandro Colomar <[email protected]>---
Hi Michael,
Now it's much better :)
Cheers,
Alex
man3/regex.3 | 42 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)man3/regex.3 +14
-0@@ -337,6 +337,48 @@ T} Thread safety MT-Safe .TE .SH CONFORMING TO POSIX.1-2001, POSIX.1-2008. +.SH EXAMPLES +.EX +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <regex.h> + +#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0])) + +static const char *const str = + "1) John Driverhacker;\en2) John Doe;\en3) John Foo;\en"; +static const char *const re = "John.*o"; + +int main(void)
man3/regex.3 T} Thread safety MT-Safe · line 363
Quoted 10 lines
man3/regex.3 +10
-0+{ + static const char *s = str; + regex_t regex; + regmatch_t pmatch[1]; + regoff_t off, len; + + if (regcomp(®ex, re, REG_NEWLINE)) + exit(EXIT_FAILURE); + + printf("String = \\"%s\\"\en", str);
Here (twice)
man3/regex.3 T} Thread safety MT-Safe · line 374
Quoted 11 lines
man3/regex.3 +11
-0+ printf("Matches:\en"); + + for (int i = 0; ; i++) { + if (regexec(®ex, s, ARRAY_SIZE(pmatch), pmatch, 0)) + break; + + off = pmatch[0].rm_so + (s \- str); + len = pmatch[0].rm_eo \- pmatch[0].rm_so; + printf("#%d:\en", i); + printf("offset = %jd; length = %jd\en", (intmax_t) off, (intmax_t) len); + printf("substring = \\"%.*s\\"\en", len, s + pmatch[0].rm_so);
And here (twice again)
Quoted 11 lines
man3/regex.3 +7
-0+ + s += pmatch[0].rm_eo; + } + + exit(EXIT_SUCCESS); +} +.EE .SH SEE ALSO .BR grep (1), .BR regex (7)
Quoted 2 lines · Alejandro Colomar
Oops, I should've used \e instead of \\ (see below). Please fix that :)
Applied and fixed. Thanks Alex!
Cheers,
Michael
Quoted 93 lines
Alex
Quoted 50 lines · Alejandro Colomar
$ gcc -Wall -Wextra -Werror -pedantic regex.c -o regex $ ./regex.3 String = "1) John Driverhacker; 2) John Doe; 3) John Foo; " Matches: #0: offset = 25; length = 7 substring = "John Do" #1: offset = 38; length = 8 substring = "John Foo"Signed-off-by Alejandro Colomar <[email protected]>---
Hi Michael,
Now it's much better :)
Cheers,
Alex
man3/regex.3 | 42 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)man3/regex.3 +14
-0@@ -337,6 +337,48 @@ T} Thread safety MT-Safe .TE .SH CONFORMING TO POSIX.1-2001, POSIX.1-2008. +.SH EXAMPLES +.EX +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <regex.h> + +#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0])) + +static const char *const str = + "1) John Driverhacker;\en2) John Doe;\en3) John Foo;\en"; +static const char *const re = "John.*o"; + +int main(void)man3/regex.3 T} Thread safety MT-Safe · line 363
Quoted 10 lines
man3/regex.3 +10
-0+{ + static const char *s = str; + regex_t regex; + regmatch_t pmatch[1]; + regoff_t off, len; + + if (regcomp(®ex, re, REG_NEWLINE)) + exit(EXIT_FAILURE); + + printf("String = \\"%s\\"\en", str);Here (twice)
man3/regex.3 T} Thread safety MT-Safe · line 374
Quoted 11 lines
man3/regex.3 +11
-0+ printf("Matches:\en"); + + for (int i = 0; ; i++) { + if (regexec(®ex, s, ARRAY_SIZE(pmatch), pmatch, 0)) + break; + + off = pmatch[0].rm_so + (s \- str); + len = pmatch[0].rm_eo \- pmatch[0].rm_so; + printf("#%d:\en", i); + printf("offset = %jd; length = %jd\en", (intmax_t) off, (intmax_t) len); + printf("substring = \\"%.*s\\"\en", len, s + pmatch[0].rm_so);And here (twice again)
Quoted 11 lines
man3/regex.3 +7
-0+ + s += pmatch[0].rm_eo; + } + + exit(EXIT_SUCCESS); +} +.EE .SH SEE ALSO .BR grep (1), .BR regex (7)
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/