Expressões regulares em C: exemplos?


171

Estou atrás de alguns exemplos simples e práticas recomendadas de como usar expressões regulares no ANSI C. man regex.hnão fornece tanta ajuda.


6
Não há suporte interno para regex no ANSI C. Que biblioteca de regex você está usando?
214 Joe Joe

7
Rob Pike escreveu uma pequena função de busca de cadeia de expressão regular que aceitou um subconjunto muito útil de expressões regulares para o livro The Practice of Programming, que ele e Brian Kernighan foram co-autores. Veja esta discussão, A Regular Expression Matcher, do Dr. Kernighan cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html
Richard Chambers

Respostas:


233

As expressões regulares, na verdade, não fazem parte do ANSI C. Parece que você pode estar falando sobre a biblioteca de expressões regulares POSIX, que vem com a maioria (todos?) * Nixes. Aqui está um exemplo do uso de expressões regulares POSIX em C (com base nisso ):

#include <regex.h>        
regex_t regex;
int reti;
char msgbuf[100];

/* Compile regular expression */
reti = regcomp(&regex, "^a[[:alnum:]]", 0);
if (reti) {
    fprintf(stderr, "Could not compile regex\n");
    exit(1);
}

/* Execute regular expression */
reti = regexec(&regex, "abc", 0, NULL, 0);
if (!reti) {
    puts("Match");
}
else if (reti == REG_NOMATCH) {
    puts("No match");
}
else {
    regerror(reti, &regex, msgbuf, sizeof(msgbuf));
    fprintf(stderr, "Regex match failed: %s\n", msgbuf);
    exit(1);
}

/* Free memory allocated to the pattern buffer by regcomp() */
regfree(&regex);

Como alternativa, você pode conferir o PCRE , uma biblioteca para expressões regulares compatíveis com Perl em C. A sintaxe Perl é praticamente a mesma sintaxe usada em Java, Python e em várias outras linguagens. A sintaxe POSIX é a sintaxe utilizada por grep, sed, vi, etc.


7
A menos que você precise evitar a dependência do segundo PCRE, ele possui alguns aprimoramentos de sintaxe e é muito estável. Pelo menos com algumas versões antigas do Linux, o "construído em" biblioteca de expressão regular não é muito difícil de bater dadas certas cadeias de entrada e de certas expressões regulares que "quase" jogo ou envolver um grande número de caracteres especiais
bdk

@ Laurence Qual é o significado de passar 0 para regcomp? O regcomp usa apenas quatro valores inteiros 1, 2, 4 e 8 para representar 4 modos diferentes.
Lixiang

2
@lixiang O último parâmetro para regcomp, cflagsé uma máscara de bits. Em pubs.opengroup.org/onlinepubs/009695399/functions/regcomp.html : "O argumento cflags é o OR inclusivo em bits de zero ou mais dos seguintes sinalizadores ...". Se você OR zero juntos, obterá 0. Vejo que a página de manual do Linux regcompdiz "cflags pode ser o bit a bit - ou de um ou mais dos seguintes", o que parece enganoso.
Laurence Gonsalves

2
Você pode extrair o texto dos grupos correspondentes com algo como: regmatch_t matches[MAX_MATCHES]; if (regexec(&exp, sz, MAX_MATCHES, matches, 0) == 0) { memcpy(buff, sz + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so); printf("group1: %s\n", buff); }observe que as correspondências do grupo começam em 1, o grupo 0 é a sequência inteira. Adicione verificações de erros fora dos limites etc.
BurnsBA 7/16

2
Quanto a se regfreeé necessário após uma falha regcomp, apesar de realmente estar subespecificado, isso sugere que isso não deve ser feito: redhat.com/archives/libvir-list/2013-September/msg00276.html
Daniel Jour

12

Provavelmente não é o que você deseja, mas uma ferramenta como re2c pode compilar expressões regulares POSIX (-ish) para ANSI C. Ele foi escrito como um substituto lex, mas essa abordagem permite sacrificar a flexibilidade e a legibilidade pelo último bit de velocidade, se você realmente precisa disso.


9

man regex.hrelata que não há entrada manual para regex.h, mas man 3 regex fornece uma página explicando as funções POSIX para correspondência de padrões.
As mesmas funções são descritas na Biblioteca GNU C: Correspondência de Expressão Regular , que explica que a Biblioteca GNU C suporta a interface POSIX.2 e a interface que a Biblioteca GNU C possui há muitos anos.

Por exemplo, para um programa hipotético que imprime quais das seqüências passadas como argumento correspondem ao padrão passado como primeiro argumento, você pode usar código semelhante ao seguinte.

#include <errno.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print_regerror (int errcode, size_t length, regex_t *compiled);

int
main (int argc, char *argv[])
{
  regex_t regex;
  int result;

  if (argc < 3)
    {
      // The number of passed arguments is lower than the number of
      // expected arguments.
      fputs ("Missing command line arguments\n", stderr);
      return EXIT_FAILURE;
    }

  result = regcomp (&regex, argv[1], REG_EXTENDED);
  if (result)
    {
      // Any value different from 0 means it was not possible to 
      // compile the regular expression, either for memory problems
      // or problems with the regular expression syntax.
      if (result == REG_ESPACE)
        fprintf (stderr, "%s\n", strerror(ENOMEM));
      else
        fputs ("Syntax error in the regular expression passed as first argument\n", stderr);
      return EXIT_FAILURE;               
    }
  for (int i = 2; i < argc; i++)
    {
      result = regexec (&regex, argv[i], 0, NULL, 0);
      if (!result)
        {
          printf ("'%s' matches the regular expression\n", argv[i]);
        }
      else if (result == REG_NOMATCH)
        {
          printf ("'%s' doesn't the regular expression\n", argv[i]);
        }
      else
        {
          // The function returned an error; print the string 
          // describing it.
          // Get the size of the buffer required for the error message.
          size_t length = regerror (result, &regex, NULL, 0);
          print_regerror (result, length, &regex);       
          return EXIT_FAILURE;
        }
    }

  /* Free the memory allocated from regcomp(). */
  regfree (&regex);
  return EXIT_SUCCESS;
}

void
print_regerror (int errcode, size_t length, regex_t *compiled)
{
  char buffer[length];
  (void) regerror (errcode, compiled, buffer, length);
  fprintf(stderr, "Regex match failed: %s\n", buffer);
}

O último argumento de regcomp()necessidades para ser pelo menos REG_EXTENDED, ou as funções irá usar expressões regulares básicas , o que significa que (por exemplo) que você precisa para usar a\{3\}em vez de a{3}usado a partir de expressões regulares estendidas , que é provavelmente o que você espera usar.

POSIX.2 tem também outra função para correspondência de curinga: fnmatch(). Ele não permite compilar a expressão regular ou obter as substrings correspondentes a uma subexpressão, mas é muito específico para verificar quando um nome de arquivo corresponde a um curinga (por exemplo, usa o FNM_PATHNAMEsinalizador).


6

Este é um exemplo do uso de REG_EXTENDED. Essa expressão regular

"^(-)?([0-9]+)((,|.)([0-9]+))?\n$"

Permite capturar números decimais no sistema espanhol e internacional. :)

#include <regex.h>
#include <stdlib.h>
#include <stdio.h>
regex_t regex;
int reti;
char msgbuf[100];

int main(int argc, char const *argv[])
{
    while(1){
        fgets( msgbuf, 100, stdin );
        reti = regcomp(&regex, "^(-)?([0-9]+)((,|.)([0-9]+))?\n$", REG_EXTENDED);
        if (reti) {
            fprintf(stderr, "Could not compile regex\n");
            exit(1);
        }

        /* Execute regular expression */
        printf("%s\n", msgbuf);
        reti = regexec(&regex, msgbuf, 0, NULL, 0);
        if (!reti) {
            puts("Match");
        }
        else if (reti == REG_NOMATCH) {
            puts("No match");
        }
        else {
            regerror(reti, &regex, msgbuf, sizeof(msgbuf));
            fprintf(stderr, "Regex match failed: %s\n", msgbuf);
            exit(1);
        }

        /* Free memory allocated to the pattern buffer by regcomp() */
        regfree(&regex);
    }

}

5

Embora a resposta acima seja boa, recomendo usar o PCRE2 . Isso significa que você pode literalmente usar todos os exemplos de regex existentes no mercado agora e não precisa traduzir de algum regex antigo.

Eu já respondi isso, mas acho que também pode ajudar aqui ..

Regex em C para procurar números de cartão de crédito

// YOU MUST SPECIFY THE UNIT WIDTH BEFORE THE INCLUDE OF THE pcre.h

#define PCRE2_CODE_UNIT_WIDTH 8
#include <stdio.h>
#include <string.h>
#include <pcre2.h>
#include <stdbool.h>

int main(){

bool Debug = true;
bool Found = false;
pcre2_code *re;
PCRE2_SPTR pattern;
PCRE2_SPTR subject;
int errornumber;
int i;
int rc;
PCRE2_SIZE erroroffset;
PCRE2_SIZE *ovector;
size_t subject_length;
pcre2_match_data *match_data;


char * RegexStr = "(?:\\D|^)(5[1-5][0-9]{2}(?:\\ |\\-|)[0-9]{4}(?:\\ |\\-|)[0-9]{4}(?:\\ |\\-|)[0-9]{4})(?:\\D|$)";
char * source = "5111 2222 3333 4444";

pattern = (PCRE2_SPTR)RegexStr;// <<<<< This is where you pass your REGEX 
subject = (PCRE2_SPTR)source;// <<<<< This is where you pass your bufer that will be checked. 
subject_length = strlen((char *)subject);




  re = pcre2_compile(
  pattern,               /* the pattern */
  PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */
  0,                     /* default options */
  &errornumber,          /* for error number */
  &erroroffset,          /* for error offset */
  NULL);                 /* use default compile context */

/* Compilation failed: print the error message and exit. */
if (re == NULL)
  {
  PCRE2_UCHAR buffer[256];
  pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
  printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset,buffer);
  return 1;
  }


match_data = pcre2_match_data_create_from_pattern(re, NULL);

rc = pcre2_match(
  re,
  subject,              /* the subject string */
  subject_length,       /* the length of the subject */
  0,                    /* start at offset 0 in the subject */
  0,                    /* default options */
  match_data,           /* block for storing the result */
  NULL);

if (rc < 0)
  {
  switch(rc)
    {
    case PCRE2_ERROR_NOMATCH: //printf("No match\n"); //
    pcre2_match_data_free(match_data);
    pcre2_code_free(re);
    Found = 0;
    return Found;
    //  break;
    /*
    Handle other special cases if you like
    */
    default: printf("Matching error %d\n", rc); //break;
    }
  pcre2_match_data_free(match_data);   /* Release memory used for the match */
  pcre2_code_free(re);
  Found = 0;                /* data and the compiled pattern. */
  return Found;
  }


if (Debug){
ovector = pcre2_get_ovector_pointer(match_data);
printf("Match succeeded at offset %d\n", (int)ovector[0]);

if (rc == 0)
  printf("ovector was not big enough for all the captured substrings\n");


if (ovector[0] > ovector[1])
  {
  printf("\\K was used in an assertion to set the match start after its end.\n"
    "From end to start the match was: %.*s\n", (int)(ovector[0] - ovector[1]),
      (char *)(subject + ovector[1]));
  printf("Run abandoned\n");
  pcre2_match_data_free(match_data);
  pcre2_code_free(re);
  return 0;
}

for (i = 0; i < rc; i++)
  {
  PCRE2_SPTR substring_start = subject + ovector[2*i];
  size_t substring_length = ovector[2*i+1] - ovector[2*i];
  printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);
  }
}

else{
  if(rc > 0){
    Found = true;

    } 
} 
pcre2_match_data_free(match_data);
pcre2_code_free(re);
return Found;

}

Instale o PCRE usando:

wget https://ftp.pcre.org/pub/pcre/pcre2-10.31.zip
make 
sudo make install 
sudo ldconfig

Compile usando:

gcc foo.c -lpcre2-8 -o foo

Verifique minha resposta para mais detalhes.

Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.