regex doesn't recognize a pattern in a string



 DEVELOP > c-Plus-Plus > regex doesn't recognize a pattern in a string

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "hogcia"
Date: 07 Nov 2007 03:54:08 AM
Object: regex doesn't recognize a pattern in a string
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 =3D "\\(Name is [a-zA-Z]+\\)"; // "\\1" na koncu
wyrazenia
oznacza=B3oby dopasowanie jeszcze raz tego, co znajdzie do wyrazenia w
nawiasach
int rc;
size_t nmatch =3D 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 !=3D (rc =3D regcomp(&preg, pattern.c_str(), REG_EXTENDED)))
{
printf("regcomp: Nie udalo sie skompilowac wyrazenia.");
exit(EXIT_FAILURE);
}
if(0 !=3D (rc =3D 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 =3D
%d\n", s.c_str(), pattern.c_str(), rc);
}
else
{
printf("W ca=B3ym wyra=BFeniu 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?
.

User: "Michael DOUBEZ"

Title: Re: regex doesn't recognize a pattern in a string 07 Nov 2007 06:12:46 AM
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
.

User: "Ron Natalie"

Title: Re: regex doesn't recognize a pattern in a string 07 Nov 2007 06:33:53 AM
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.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER