OVH Cloud OVH Cloud

question newbie pcre

4 réponses
Avatar
pascal
Voilà, j'utilise <pcre.h> pour récupèrer le nombre de lignes d'un message
dans son en-tête.

pcre *re_lignes = pcre_compile(
"(Lines:) ([0-9]{1,4})",
etc....
<------------>
ensuite:
if (pcre_exec(

re_lignes, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
data, /* the subject string */
length, /* the length of the subject */
0, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
OVECCOUNT)>=0)
{
char *substring_start = data + ovector[2*2];
int substring_length = ovector[2*2+1] - ovector[2*2];
printf("lignes = %.*s\n", substring_length, substring_start);
printf("Match succeeded at offset %d\n", ovector[0]);
<-------------->
ma question est: comment je fais pour mettre mon "lignes" du printf("lignes
= %.*s\n", substring_length, substring_start); dans un int par exemple ?

merci de toute aide

Cdt

4 réponses

Avatar
Laurent Wacrenier
pascal écrit:
Voilà, j'utilise <pcre.h> pour récupèrer le nombre de lignes d'un message
dans son en-tête.

pcre *re_lignes = pcre_compile(
"(Lines:) ([0-9]{1,4})",


Fainéant.

ma question est: comment je fais pour mettre mon "lignes" du printf("lignes
= %.*sn", substring_length, substring_start); dans un int par exemple ?


Avec strtol.

Si tu veux te limiter aux 4 premiers chiffres, quelque chose comme
ça :

(substring_start[0] - '0') +
(substring_length < 1) ? 0 : 10 * ( substring_start[1] - '0' +
(substring_length < 2) ? 0 : 10 * ( substring_start[2] - '0' +
(substring_length < 3) ? 0 : 10 * ( substring_start[3] - '0' )))

Avatar
pascal
merci ! je vais essayer !

"Laurent Wacrenier" <lwa@ teaser . fr> wrote in message
news:
pascal écrit:
Voilà, j'utilise <pcre.h> pour récupèrer le nombre de lignes d'un
message


dans son en-tête.

pcre *re_lignes = pcre_compile(
"(Lines:) ([0-9]{1,4})",


Fainéant.

ma question est: comment je fais pour mettre mon "lignes" du
printf("lignes


= %.*sn", substring_length, substring_start); dans un int par exemple ?


Avec strtol.

Si tu veux te limiter aux 4 premiers chiffres, quelque chose comme
ça :

(substring_start[0] - '0') +
(substring_length < 1) ? 0 : 10 * ( substring_start[1] - '0' +
(substring_length < 2) ? 0 : 10 * ( substring_start[2] - '0' +
(substring_length < 3) ? 0 : 10 * ( substring_start[3] - '0' )))



Avatar
pascal
merci bcp pour ton aide ;-))

long a= strtol( substring_start, NULL, 0);
printf("a= %ldn", a); /* juste pour voir si ça marche */
Avatar
Laurent Wacrenier
pascal écrit:
long a= strtol( substring_start, NULL, 0);


"0" c'est pour l'octal ou le décimal. Pour le lire en décimal

strtol( substring_start, NULL, 10)