Regular Expressions in C - Linux

This is a discussion on Regular Expressions in C - Linux ; Hi guys, I have a problem with RE in C and I want to know if you can help me. I want to split a address in -> address, number and complementantion example R ITARIPANDUVA 84 CJ 11 address = ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Regular Expressions in C

  1. Regular Expressions in C

    Hi guys, I have a problem with RE in C and I want to know if you can
    help me.
    I want to split a address in -> address, number and complementantion
    example

    R ITARIPANDUVA 84 CJ 11

    address = R ITAPIRANDUVA
    numer = 84
    complementation = CJ 11

    I using the RE ^(\w+)(\d+)(.+)?$
    And I using the regex.h, building the regex_t with the function
    regcomp and using the regexec to split the address, but the regexec
    returns the RE can't work with the address.
    The code:
    void er_match(char *argv[])
    {
    static char str[]="R ITAPURANDA 84";
    /* aloca espaço para a estrutura do tipo regex_t */
    regex_t reg;

    /* compila a ER passada em argv[1]
    * em caso de erro, a função retorna diferente de zero */
    /*if (regcomp(&reg , argv[1], REG_EXTENDED|REG_NOSUB) != 0)
    {*/
    if (regcomp(&reg , argv[1], 0) != 0) {
    fprintf(stderr,"error regcomp\n");
    exit(1);
    }
    printf("%s\n",argv[1]);
    printf("%s\n",str);
    /* tenta casar a ER compilada (&reg) com a entrada (argv[2])
    * se a função regexec retornar 0 casou, caso contrário não */
    if ((regexec(&reg, str, 0, (regmatch_t *)NULL, 0)) == 0)
    printf("It Find\n");
    else
    printf("It Dont't Find\n");

    }

    int main(int argc, char **argv)
    {
    if (argc != 3) {
    fprintf(stderr,"usage: match \n");
    return 1;
    }
    er_match(argv);
    return 0;

    }

    Thanks

  2. Re: Regular Expressions in C

    On 10 Dec, 18:58, Arthur wrote:
    > Hi guys, I have a problem with RE in C and I want to know if you can
    > help me.
    > I want to split a address in -> address, number and complementantion
    > example
    >
    > R ITARIPANDUVA 84 CJ 11
    >
    > address = R ITAPIRANDUVA
    > numer = 84
    > complementation = CJ 11
    >
    > I using the RE ^(\w+)(\d+)(.+)?$
    > And I using the regex.h, building the regex_t with the function
    > regcomp and using the regexec to split the address, but the regexec
    > returns the RE can't work with the address.
    > The code:
    > void er_match(char *argv[])
    > {
    > static char str[]="R ITAPURANDA 84";
    > /* aloca espaço para a estrutura do tipo regex_t */
    > regex_t reg;
    >
    > /* compila a ER passada em argv[1]
    > * em caso de erro, a função retorna diferente de zero */
    > /*if (regcomp(&reg , argv[1], REG_EXTENDED|REG_NOSUB) != 0)
    > {*/
    > if (regcomp(&reg , argv[1], 0) != 0) {
    > fprintf(stderr,"error regcomp\n");
    > exit(1);
    > }
    > printf("%s\n",argv[1]);
    > printf("%s\n",str);
    > /* tenta casar a ER compilada (&reg) com a entrada (argv[2])
    > * se a função regexec retornar 0 casou, caso contrário não */
    > if ((regexec(&reg, str, 0, (regmatch_t *)NULL, 0)) == 0)
    > printf("It Find\n");
    > else
    > printf("It Dont't Find\n");
    >
    > }
    >
    > int main(int argc, char **argv)
    > {
    > if (argc != 3) {
    > fprintf(stderr,"usage: match \n");
    > return 1;
    > }
    > er_match(argv);
    > return 0;
    >
    > }
    >
    > Thanks


    The following is less than ideal, but it's
    a step in the right direction:

    #include
    #include
    #include

    void
    er_match( char **argv )
    {
    int err;
    char buf[ BUFSIZ ];
    char str[] = "R ITAPURANDA 84 CJ11";
    regex_t reg;
    regmatch_t matches[ 4 ];

    if( ( err = regcomp( &reg, argv[1], REG_EXTENDED )) ) {
    regerror( err, &reg, buf, BUFSIZ );
    fprintf( stderr, "%s\n", buf );
    exit( EXIT_FAILURE );
    }
    if( ( regexec( &reg, str, 4, matches, 0 ) ) == 0 ) {
    int n;
    if( -1 != ( n = matches[ 1 ].rm_so))
    printf( "address: %s\n", str + n );
    if( -1 != ( n = matches[ 2 ].rm_so))
    printf( "number : %s\n", str + n );
    if( -1 != ( n = matches[ 3 ].rm_so))
    printf( "complem: %s\n", str + n );

    }
    else
    printf( "not found\n" );

    }

    int
    main( int argc, char **argv )
    {
    er_match( argv );
    return EXIT_SUCCESS;
    }


    $ ./a.out '^([^[:digit:]]*)([[:digit:]]+)([^[:digit:]].*)'
    address: R ITAPURANDA 84 CJ11
    number : 84 CJ11
    complem: CJ11

  3. Re: Regular Expressions in C

    On Dec 10, 6:35 pm, William Pursell wrote:
    > On 10 Dec, 18:58, Arthur wrote:
    >
    >
    >
    > > Hi guys, I have a problem with RE in C and I want to know if you can
    > > help me.
    > > I want to split a address in -> address, number and complementantion
    > > example

    >
    > > R ITARIPANDUVA 84 CJ 11

    >
    > > address = R ITAPIRANDUVA
    > > numer = 84
    > > complementation = CJ 11

    >
    > > I using the RE ^(\w+)(\d+)(.+)?$
    > > And I using the regex.h, building the regex_t with the function
    > > regcomp and using the regexec to split the address, but the regexec
    > > returns the RE can't work with the address.
    > > The code:
    > > void er_match(char *argv[])
    > > {
    > > static char str[]="R ITAPURANDA 84";
    > > /* aloca espaço para a estrutura do tipo regex_t */
    > > regex_t reg;

    >
    > > /* compila a ER passada em argv[1]
    > > * em caso de erro, a função retorna diferente de zero */
    > > /*if (regcomp(&reg , argv[1], REG_EXTENDED|REG_NOSUB) != 0)
    > > {*/
    > > if (regcomp(&reg , argv[1], 0) != 0) {
    > > fprintf(stderr,"error regcomp\n");
    > > exit(1);
    > > }
    > > printf("%s\n",argv[1]);
    > > printf("%s\n",str);
    > > /* tenta casar a ER compilada (&reg) com a entrada (argv[2])
    > > * se a função regexec retornar 0 casou, caso contrário não */
    > > if ((regexec(&reg, str, 0, (regmatch_t *)NULL, 0)) == 0)
    > > printf("It Find\n");
    > > else
    > > printf("It Dont't Find\n");

    >
    > > }

    >
    > > int main(int argc, char **argv)
    > > {
    > > if (argc != 3) {
    > > fprintf(stderr,"usage: match \n");
    > > return 1;
    > > }
    > > er_match(argv);
    > > return 0;

    >
    > > }

    >
    > > Thanks

    >
    > The following is less than ideal, but it's
    > a step in the right direction:
    >
    > #include
    > #include
    > #include
    >
    > void
    > er_match( char **argv )
    > {
    > int err;
    > char buf[ BUFSIZ ];
    > char str[] = "R ITAPURANDA 84 CJ11";
    > regex_t reg;
    > regmatch_t matches[ 4 ];
    >
    > if( ( err = regcomp( &reg, argv[1], REG_EXTENDED )) ) {
    > regerror( err, &reg, buf, BUFSIZ );
    > fprintf( stderr, "%s\n", buf );
    > exit( EXIT_FAILURE );
    > }
    > if( ( regexec( &reg, str, 4, matches, 0 ) ) == 0 ) {
    > int n;
    > if( -1 != ( n = matches[ 1 ].rm_so))
    > printf( "address: %s\n", str + n );
    > if( -1 != ( n = matches[ 2 ].rm_so))
    > printf( "number : %s\n", str + n );
    > if( -1 != ( n = matches[ 3 ].rm_so))
    > printf( "complem: %s\n", str + n );
    >
    > }
    > else
    > printf( "not found\n" );
    >
    > }
    >
    > int
    > main( int argc, char **argv )
    > {
    > er_match( argv );
    > return EXIT_SUCCESS;
    >
    > }
    >
    > $ ./a.out '^([^[:digit:]]*)([[:digit:]]+)([^[:digit:]].*)'
    > address: R ITAPURANDA 84 CJ11
    > number : 84 CJ11
    > complem: CJ11


    thanks William

+ Reply to Thread