diff mbox series

[v2] regex.3: Add example program

Message ID 20201017132748.150322-1-colomar.6.4.3@gmail.com
State Not Applicable
Headers show
Series [v2] regex.3: Add example program | expand

Commit Message

Alejandro Colomar Oct. 17, 2020, 1:27 p.m. UTC
$ 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(+)

Comments

Alejandro Colomar Oct. 17, 2020, 1:37 p.m. UTC | #1
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)
>
Michael Kerrisk \(man-pages\) Oct. 17, 2020, 2:35 p.m. UTC | #2
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)
>>
diff mbox series

Patch

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)