unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar via Libc-alpha <libc-alpha@sourceware.org>
To: mtk.manpages@gmail.com
Cc: Alejandro Colomar <colomar.6.4.3@gmail.com>,
	linux-man@vger.kernel.org, libc-alpha@sourceware.org
Subject: [PATCH v2] regex.3: Add example program
Date: Sat, 17 Oct 2020 15:27:49 +0200	[thread overview]
Message-ID: <20201017132748.150322-1-colomar.6.4.3@gmail.com> (raw)
In-Reply-To: <efc1c2c5-3cf9-e755-cab3-c19e100f210b@gmail.com>

$ 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 <colomar.6.4.3@gmail.com>
---

Hi Michael,

Now it's much better :)

Cheers,

Alex

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

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


  reply	other threads:[~2020-10-17 13:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-17 13:13 [PATCH] regex.3: Add example program Alejandro Colomar via Libc-alpha
2020-10-17 13:17 ` Alejandro Colomar via Libc-alpha
2020-10-17 13:27   ` Alejandro Colomar via Libc-alpha [this message]
2020-10-17 13:37     ` [PATCH v2] " Alejandro Colomar via Libc-alpha
2020-10-17 14:35       ` Michael Kerrisk (man-pages) via Libc-alpha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201017132748.150322-1-colomar.6.4.3@gmail.com \
    --to=libc-alpha@sourceware.org \
    --cc=colomar.6.4.3@gmail.com \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).