2009-03-11 12 views

答えて

2

いいえ、regexec()は呼び出しごとに1つの一致を検出します。次の試合を見たい場合は、もう一度試合に出さなければなりません。

プレーンな部分文字列のみを検索する場合は、標準のCのstring.h関数を使用するほうがはるかに優れています。strstr();特殊正規表現のエスケープについて心配する必要はありません。

0

regexecは、4番目のパラメータpmatchにすべての一致がある構造体を返します。 "pmatch"は固定サイズの構造体です。より多くのマッチがある場合は、別の時間に関数を呼び出すことになります。

私は2つのネストされたループでこのコードを見つけました。私はそれを変更しました。あなたはhttp://www.lemoda.net/c/unix-regex/index.htmlでそれを見つけるCAND元タラ:私はない50の評判を持っているので、別の答えを作成するための

static int match_regex (regex_t * r, const char * to_match) 
{ 
    /* "P" is a pointer into the string which points to the end of the 
     previous match. */ 
    const char * p = to_match; 
    /* "N_matches" is the maximum number of matches allowed. */ 
    const int n_matches = 10; 
    /* "M" contains the matches found. */ 
    regmatch_t m[n_matches]; 
    int number_of_matches = 0; 
    while (1) { 
     int i = 0; 
     int nomatch = regexec (r, p, n_matches, m, 0); 
     if (nomatch) { 
      printf ("No more matches.\n"); 
      return nomatch; 
     } 
     for (i = 0; i < n_matches; i++) { 
      if (m[i].rm_so == -1) { 
       break; 

      } 
      number_of_matches ++; 
     } 
     p += m[0].rm_eo; 
    } 
    return number_of_matches ; 
} 
0

申し訳ありません。 @Oscar Raig Colonの答えはコメントできません。

pmatchすべての部分文字列に一致することはできません。pmatchは部分式のオフセットを保存するために使用され、キーは部分式を理解するためのもので、部分式はBREで "\(\)"、EREで "(正規表現全体に部分式がない場合、regexec()は最初の一致文字列のオフセットだけを返し、pmatch [0]に置きます。

あなたに例を見つけることができる[http://pubs.opengroup.org/onlinepubs/007908799/xsh/regcomp.html][1]

以下にREG_NOTBOLフラグは、ユーザによって供給されたパターンと一致する行のすべてのサブストリングを検索するためにregexecは()と共に使用することができる方法を示しています。 (例を簡単にするために、ほとんどエラーチェックは行われません)

(void) regcomp (&re, pattern, 0); 
/* this call to regexec() finds the first match on the line */ 
error = regexec (&re, &buffer[0], 1, &pm, 0); 
while (error == 0) { /* while matches found */ 
    /* substring found between pm.rm_so and pm.rm_eo */ 
    /* This call to regexec() finds the next match */ 
    error = regexec (&re, buffer + pm.rm_eo, 1, &pm, REG_NOTBOL); 
} 
関連する問題