Linux-man Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] regex.3: Add example program
@ 2020-10-17 13:13 Alejandro Colomar
  2020-10-17 13:17 ` Alejandro Colomar
  0 siblings, 1 reply; 5+ messages in thread
From: Alejandro Colomar @ 2020-10-17 13:13 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man, libc-alpha

$ 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)
-- 
2.28.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] regex.3: Add example program
  2020-10-17 13:13 [PATCH] regex.3: Add example program Alejandro Colomar
@ 2020-10-17 13:17 ` Alejandro Colomar
  2020-10-17 13:27   ` [PATCH v2] " Alejandro Colomar
  0 siblings, 1 reply; 5+ messages in thread
From: Alejandro Colomar @ 2020-10-17 13:17 UTC (permalink / raw)
  To: mtk.manpages; +Cc: linux-man, libc-alpha

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)
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] regex.3: Add example program
  2020-10-17 13:17 ` Alejandro Colomar
@ 2020-10-17 13:27   ` Alejandro Colomar
  2020-10-17 13:37     ` Alejandro Colomar
  0 siblings, 1 reply; 5+ messages in thread
From: Alejandro Colomar @ 2020-10-17 13:27 UTC (permalink / raw)
  To: mtk.manpages; +Cc: Alejandro Colomar, linux-man, libc-alpha

$ 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] regex.3: Add example program
  2020-10-17 13:27   ` [PATCH v2] " Alejandro Colomar
@ 2020-10-17 13:37     ` Alejandro Colomar
  2020-10-17 14:35       ` Michael Kerrisk (man-pages)
  0 siblings, 1 reply; 5+ messages in thread
From: Alejandro Colomar @ 2020-10-17 13:37 UTC (permalink / raw)
  To: mtk.manpages; +Cc: linux-man, libc-alpha

Oops,  I should've used \e instead of \\ (see below).
Please fix that :)

Thanks,

Alex

On 10/17/20 3:27 PM, Alejandro Colomar wrote:
> $ 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);


Here (twice)

> +    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);


And here (twice again)

> +
> +        s += pmatch[0].rm_eo;
> +    }
> +
> +    exit(EXIT_SUCCESS);
> +}
> +.EE
>  .SH SEE ALSO
>  .BR grep (1),
>  .BR regex (7)
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] regex.3: Add example program
  2020-10-17 13:37     ` Alejandro Colomar
@ 2020-10-17 14:35       ` Michael Kerrisk (man-pages)
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Kerrisk (man-pages) @ 2020-10-17 14:35 UTC (permalink / raw)
  To: Alejandro Colomar; +Cc: mtk.manpages, linux-man, libc-alpha

On 10/17/20 3:37 PM, Alejandro Colomar wrote:
> Oops,  I should've used \e instead of \\ (see below).
> Please fix that :)

Applied and fixed. Thanks Alex!

Cheers,

Michael

> Alex
> 
> On 10/17/20 3:27 PM, Alejandro Colomar wrote:
>> $ 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);
> 
> 
> Here (twice)
> 
>> +    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);
> 
> 
> And here (twice again)
> 
>> +
>> +        s += pmatch[0].rm_eo;
>> +    }
>> +
>> +    exit(EXIT_SUCCESS);
>> +}
>> +.EE
>>  .SH SEE ALSO
>>  .BR grep (1),
>>  .BR regex (7)
>>


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-17 13:13 [PATCH] regex.3: Add example program Alejandro Colomar
2020-10-17 13:17 ` Alejandro Colomar
2020-10-17 13:27   ` [PATCH v2] " Alejandro Colomar
2020-10-17 13:37     ` Alejandro Colomar
2020-10-17 14:35       ` Michael Kerrisk (man-pages)

Linux-man Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-man/0 linux-man/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-man linux-man/ https://lore.kernel.org/linux-man \
		linux-man@vger.kernel.org
	public-inbox-index linux-man

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-man


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git