2009-03-11 5 views

답변

2

아니요, regexec()는 호출 당 하나의 일치 항목 만 찾습니다. 다음 경기를 찾으려면 문자열을 따라 다시 호출해야합니다.

평범한 하위 문자열 만 검색하려는 경우 표준 C 문자열을 사용하는 것이 훨씬 낫습니다. function strstr(); 그러면 특별한 정규 표현식 문자를 빠져 나오는 것에 대해 걱정할 필요가 없습니다.

0

regexec은 네 번째 매개 변수 인 "pmatch"에서 모든 일치 항목이있는 구조를 반환합니다. "pmatch"는 고정 크기 구조입니다. 더 많은 일치 항목이 있으면 다른 시간에 함수를 호출합니다.

이 코드를 두 개의 중첩 루프와 함께 발견했으며이를 수정했습니다. 당신이 http://www.lemoda.net/c/unix-regex/index.html에서 찾을 모시의 원래 대구 : 내가하지 (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); 
}