J'utilise la bibliothèque regex PCRE en C (http://www.pcre.org/) pour analyser et faire correspondre ma chaîne HTML. Pour simplifier ma question, supposons que je suis la chaîne source: "aaa: bbbb:", et mon modèle: un (*.?): | B:, le symbole (*.?)? indiquent que c'est un match non-gourmand, de sorte que la réponse devrait être deux matchups: l'une est "aaa:" et l'autre "bbbb:",Réponse étrange en utilisant PCRE Regex
je PROGRAMMÉS:
char *src = "aaa: bbbb:";
char *pattern = "a(.*?):|b(.*?):";
pcre *re = NULL;
//---missing out---
re = pcre_compile(pattern, // pattern,
0, // options,
&error, // errptr,
&erroffset, // erroffset,
NULL); // tableptr,
while (
(rc = pcre_exec(re, // regex ptr,
NULL, // extra arg,
src, // subject,
strlen(src), // length,
0, // startoffset,
0, // options,
ovector, // ovector,
OVECCOUNT) // ovecsize,
)!=PCRE_ERROR_NOMATCH)
{
printf("\nOK, string has matched ...there are %d matchups\n\n",rc); //
for (i = 0; i < rc; i++)
{
char *substring_start = src + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("$%2d: %.*s length: %d\n", i, substring_length, substring_start,substring_length);
}
src = src + ovector[1]; // to move the src pointer to the end offset of current matchup
if (!src) break;
}
pcre_free(re);
Je suis mon résultat:
Source : aaa: bbbb:
Pattern: "a(.*?):|b(.*?):"
OK, string has matched ...there are 2 matches
$ 0: aaa: length: 4
$ 1: aa length: 2
OK, string has matched ...there are 3 matches
$ 0: bbbb: length: 5
$ 1: length: 0
$ 2: bbb length: 3
Et je me demande, comment ai-je la réponse "$ 1: longueur: 0"?
// -------------------------------------------- --------------------------------------------
@Jonathan Leffler Je pense que ta réponse est juste.
En ce moment j'ai essayé
Source: "aaa: bbb: ccc:"
Pattern: "c(.+?):|a(.+?):|b(.+?):"
et a obtenu le résultat se présente comme suit:
$ 0: aaa: length: 4
$ 1: length: 0
$ 2: aa length: 2
$ 0: bbbb: length: 5
$ 1: length: 0
$ 2: length: 0
$ 3: bbb length: 3
$ 0: cccc: length: 5
$ 1: ccc length: 3
qui prouve votre réponse dans le sens inverse:
La capture de l'expression régulière arrête quand un match est trouvé, donc aaa:
a été capturé avec a(.+?):
après avoir essayé match c(.+?):
, et la première ligne du résultat montre la chaîne entière , Le n ° 2 montre le résultat de décalage correspondant avec l'alternative c(.+?):
B (. +?), Il fut capturé enfin dans l'expression rationnelle, qui explique les deux length : 0
Pour c (. +?), il a été capturé en premier lieu, donc il n'y a pas length : 0
Pourriez-vous augmenter l'exemple de source afin qu'il compile? – thuovila
Peut-être parce que vous utilisez '*' au lieu de '+' – jcubic