regex doesn't recognize a pattern in a string

Discussion in 'C++' started by hogcia, Nov 7, 2007.

  1. hogcia

    hogcia Guest

    Hello!
    I've got a little problem - I'm writing a program in C++, which should
    compare a text input from keyboard with a regular expression and
    return what parttern was recognized and where. The problem is the
    program doesn't find a pattern even if it actually is in the string.
    This is the code:

    #include <regex.h>
    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    regex_t preg;
    string s;
    string pattern = "\\(Name is [a-zA-Z]+\\)"; // "\\1" na koncu
    wyrazenia
    oznacza³oby dopasowanie jeszcze raz tego, co znajdzie do wyrazenia w
    nawiasach
    int rc;
    size_t nmatch = 2;
    regmatch_t pmatch[2];

    cout << "Podaj string, w ktorym bedzie wyszukany wzorzec\n";

    getline(cin, s); //wczytuje do konca linii - w przeciwienstwie do
    cin >>,
    ktore wczytuje do spacji
    cout << s << endl;

    if(0 != (rc = regcomp(&preg, pattern.c_str(), REG_EXTENDED)))
    {
    printf("regcomp: Nie udalo sie skompilowac wyrazenia.");
    exit(EXIT_FAILURE);
    }

    if(0 != (rc = regexec(&preg, s.c_str(), nmatch, pmatch, 0)) ) //
    funkcja
    c_str zamienia typ string na const char *
    {
    printf("regexec: Nie udalo sie dopasowac stringu %s do
    wyrazenia %s, rc =
    %d\n", s.c_str(), pattern.c_str(), rc);
    }
    else
    {
    printf("W ca³ym wyra¿eniu dopasowano wzorzec: \"%.*s\" na
    pozycji od %d do
    %d\n", pmatch[0].rm_eo-pmatch[0].rm_so, &s[pmatch[0].rm_so],
    pmatch[0].rm_so, pmatch[0].rm_eo - 1);
    }

    regfree(&preg);

    return 0;
    }

    Any suggestions?
     
    hogcia, Nov 7, 2007
    #1
    1. Advertising

  2. hogcia a écrit :
    > Hello!
    > I've got a little problem - I'm writing a program in C++, which should
    > compare a text input from keyboard with a regular expression and
    > return what parttern was recognized and where. The problem is the
    > program doesn't find a pattern even if it actually is in the string.
    > This is the code:
    > [snip]
    > regex_t preg;
    > string s;
    > string pattern = "\\(Name is [a-zA-Z]+\\)"; // "\\1" na koncu


    > [snip]
    >
    > if(0 != (rc = regcomp(&preg, pattern.c_str(), REG_EXTENDED)))
    > [snip]
    >
    > Any suggestions?
    >


    You are using EXTENDED regular expression so the ( is special by default.
    your pattern should be:
    string pattern = "(Name is [a-zA-Z]+)";

    Michael
     
    Michael DOUBEZ, Nov 7, 2007
    #2
    1. Advertising

  3. hogcia

    Ron Natalie Guest

    hogcia wrote:
    > Hello!
    > I've got a little problem - I'm writing a program in C++, which should
    > compare a text input from keyboard with a regular expression and
    > return what parttern was recognized and where. The problem is the
    > program doesn't find a pattern even if it actually is in the string.
    > This is the code:
    >

    OK, now give us the input you gave it, what you were expecting it to
    say, and what you observed.
     
    Ron Natalie, Nov 7, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    1
    Views:
    976
    Ilias Lazaridis
    Sep 22, 2006
  2. Xah Lee
    Replies:
    8
    Views:
    490
    Ilias Lazaridis
    Sep 26, 2006
  3. Jerric
    Replies:
    16
    Views:
    830
    Daniel Pitts
    Jan 9, 2012
  4. Xah Lee
    Replies:
    2
    Views:
    243
    Xah Lee
    Sep 25, 2006
  5. ChrisC
    Replies:
    4
    Views:
    191
    ChrisC
    Jun 25, 2010
Loading...

Share This Page