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? 2 2035
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jon Maz |
last post by:
Hi All,
Am getting frustrated trying to port the following (pretty simple) function
to CSharp. The problem is that I'm lousy at Regular Expressions....
//from...
|
by: aevans1108 |
last post by:
expanding this message to microsoft.public.dotnet.xml
Greetings
Please direct me to the right group if this is an inappropriate place
to post this question. Thanks.
I want to format a...
|
by: Tim Conner |
last post by:
Hi,
Thanks to Peter, Chris and Steven who answered my previous answer about
regex to split a string. Actually, it was as easy as create a regex with the
pattern "/*-+()," and most of my string...
|
by: Mortimer Schnurd |
last post by:
Hi All,
I am a VB 6 programmer who is now trying to learn C#. In doing so, I
am trying to convert some of my VB modules to C#. I routinely user Reg
Expressions in VB and am having some trouble...
|
by: bill tie |
last post by:
I'd appreciate it if you could advise.
1. How do I replace "\" (backslash) with anything?
2. Suppose I want to replace
(a) every occurrence of characters "a", "b", "c", "d" with "x",
(b)...
|
by: Dave |
last post by:
I'm struggling with something that should be fairly simple. I just don't
know the regext syntax very well, unfortunately.
I'd like to parse words out of what is basically a boolean search...
|
by: lgbjr |
last post by:
Hi All,
I'm trying to split a string on every character. The string happens to be a
representation of a hex number. So, my regex expression is ().
Seems simple, but for some reason, I'm not...
|
by: Nightcrawler |
last post by:
Hi all,
I am trying to use regular expressions to parse out mp3 titles into
three different groups (artist, title and remix). I currently have
three ways to name a mp3 file:
Artist - Title ...
|
by: nuffnough |
last post by:
I am doing a string.replace in a simple table generation app I wrote,
and I can't figure out how to match whitespace with /s, so I thought
I would see if osmeone where would be kind enough to...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |