diff mbox series

regex.3: Add example program

Message ID 20201017131324.123039-1-colomar.6.4.3@gmail.com
State New
Headers show
Series regex.3: Add example program | expand

Commit Message

Alejandro Colomar Oct. 17, 2020, 1:13 p.m. UTC
$ 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 <colomar.6.4.3@gmail.com>
---

Hello Michael,

Here's the example for regex.3 :-}

Best regards,

Alex


 man3/regex.3 | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

Comments

Alejandro Colomar Oct. 17, 2020, 1:17 p.m. UTC | #1
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

On 10/17/20 3:13 PM, Alejandro Colomar wrote:
> $ 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 <colomar.6.4.3@gmail.com>
> ---
> 
> Hello Michael,
> 
> Here's the example for regex.3 :-}
> 
> Best regards,
> 
> Alex
> 
> 
>  man3/regex.3 | 44 ++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 44 insertions(+)
> 
> diff --git a/man3/regex.3 b/man3/regex.3
> index 7c5132995..94da88b7e 100644
> --- a/man3/regex.3
> +++ b/man3/regex.3
> @@ -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(&regex, re, REG_NEWLINE))
> +        exit(EXIT_FAILURE);
> +
> +    printf("String = \\"%s\\"\en", str);
> +
> +    for (ptrdiff_t i = 0; ; i++) {
> +        if (regexec(&regex, 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)
>
diff mbox series

Patch

diff --git a/man3/regex.3 b/man3/regex.3
index 7c5132995..94da88b7e 100644
--- a/man3/regex.3
+++ b/man3/regex.3
@@ -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(&regex, re, REG_NEWLINE))
+        exit(EXIT_FAILURE);
+
+    printf("String = \\"%s\\"\en", str);
+
+    for (ptrdiff_t i = 0; ; i++) {
+        if (regexec(&regex, 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)