From the following code, I expect to get this output from the corresponding input:
Input: FOO Output: Match
Input: FOOBAR Output: Match
Input: BAR Output: No Match
Input: fOOBar Output: No Match
But why it gives "No Match" for input FOOBAR?
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
int main ( int arg_count, char *arg_vec[] ) {
if (arg_count !=2 ) {
cerr << "expected one argument" << endl;
return EXIT_FAILURE;
}
string InputString = arg_vec[1];
string toMatch = "FOO";
const regex e(toMatch);
if (regex_match(InputString, e,match_partial)) {
cout << "Match" << endl;
} else {
cout << "No Match" << endl;
}
return 0;
}
Update:
Finally it works with the following approach:
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
bool testSearchBool(const boost::regex &ex, const string st) {
cout << "Searching " << st << endl;
string::const_iterator start, end;
start = st.begin();
end = st.end();
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
return boost::regex_search(start, end, what, ex, flags);
}
int main ( int arg_count, char *arg_vec[] ) {
if (arg_count !=2 ) {
cerr << "expected one argument" << endl;
return EXIT_FAILURE;
}
string InputString = arg_vec[1];
string toMatch = "FOO*";
static const regex e(toMatch);
if ( testSearchBool(e,InputString) ) {
cout << "MATCH" << endl;
}
else {
cout << "NOMATCH" << endl;
}
return 0;
}
Use regex_search instead of regex_match.
Your regular expression has to account for characters at the beginning and end of the sub-string "FOO".
I'm not sure but "FOO*" might do the trick
match_partial would only return true if the partial string was found at the end of the text input, not the beginning.
A partial match is one that matched
one or more characters at the end of
the text input, but did not match all
of the regular expression (although it
may have done so had more input been
available)
So FOOBAR matched with "FOO" would return false.
As the other answer suggests, using regex.search would allow you to search for sub-strings more effectively.
Related
I'm using the following code which is similar to Stroustrup's C++ 4th Edition Page 127&128. Per output log below, it prints the first match, however not the match for the trailing -XXXX digits.
Does anyone know why the trailing digits are not matched and/or printed??
Thanks
#include <iostream>
#include <regex>
using namespace std;
int main(int argc, char *argv[])
{
// ZIP code pattern: XXddddd-dddd and variants
regex pat (R"(\w{2}\s*\d{5}(−\d{4})?)");
int lineno = 0;
for (string line; getline(cin,line);) {
++lineno;
smatch matches; // matched strings go here
if (regex_search(line, matches, pat)) // search for pat in line
for (auto p : matches) {
cout << p << " ";
}
cout << endl;
// cout << lineno << ": " << matches[0] << '\n';
}
return 0;
}
Output log:
$ ./a.out
AB00000-0000
AB00000
− is not -. That are two different symbols. You have − in the code and - in the input. Here I fixed the code:
#include <iostream>
#include <regex>
using namespace std;
int main(int argc, char *argv[])
{
// ZIP code pattern: XXddddd-dddd and variants
regex pat (R"(\w{2}\s*\d{5}(-\d{4})?)");
int lineno = 0;
for (string line; getline(cin,line);) {
++lineno;
smatch matches; // matched strings go here
if (regex_search(line, matches, pat)) // search for pat in line
for (auto p : matches) {
cout << p << " ";
}
cout << endl;
// cout << lineno << ": " << matches[0] << '\n';
}
return 0;
}
I need a C++ code for the following problem:
i have a text file that i want to start reading from a specific line, then i need to print the output located between the characters --- <\s>
example: hello<\s>
i want the output to be hello
I think i should use text parser but not sure how!
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
std::string line_;
ifstream file_("tty.txt");
if (file_.is_open())
{
while (getline(file_, line_))
{
std::cout << line_ << '\n';
}
file_.close();
}
else
std::cout << "error" << '\n';
std::cin.get();
system("PAUSE");
return 0;
}
You can load all text in one variable, and then with regex search all occurences of your desired pattern (in your case <sth>(any_aplha_numeric_character)*</sth> where * means one or more occurence, you can read about it at any std::regex tutorial)
Example:
std::smatch m;
std::string text = "<a>adsd</a> <a>esd</a>";
std::string::const_iterator searchStart(text.cbegin());
std::regex rgx("<a>[A-Za-z0-9\\s]*</a>");
while (std::regex_search(searchStart, text.cend(), m, rgx))
{
cout << m[0] << endl;
searchStart += m.position() + m.length();
}
gives: <a>adsd</a> and <a>esd</a> as a result, from which is very easy to extract that inner string
The valid charaters are
// ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/
#include <iostream>
#include <string>
using namespace std;
int main();
{
bool bIsValid = true;
// test characters
string strCheck("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/");
string s("foo#?+baa") ; // should bring a "false" because of the "#?+" characters
string::const_iterator it = strCheck.begin();
// this is NOT a clever soulution has anybody a better idea ?
while (s.find(*it) != string::npos)
{
++it;
if(!s.find((*it))
{
bIsValidKey = false;
break;
}
}
cout << "Is Valid: " << bIsValid << endl ;
}
My problem is how can a get the first charater after the iteratorpoint to compare
with the allowed charactes. I need something like (*it).first_charater_after to
solve the problem.
Dos anybody has an other idea to check that in the string only exists a defined
number of charaters?
Use string::find_first_not_of?
Try this:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
bool bIsValid = true;
string s("GHHbaa111__") ; // Add/remove other characters
regex r("^[[:alnum:]_]*$");
if (regex_match(s,r)) {
cout << "Valid string" << endl;
} else {
cout << "Invalid string" << endl;
}
}
#include <iostream>
#include <string>
int main() {
bool bIsValid = true;
// test characters
std::string strCheck("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/");
std::string s("foo#?+baa") ; // should bring a "false" because of the "#?+" characters
if (s.find_first_not_of(strCheck) != std::string::npos)
bIsValid = false;
std::cout << "Is Valid: " << bIsValid << std::endl ;
}
I need to parse a date-time string like 2012-12-21 12:10:35 into a time_t value using boost::spirit. here is my code snippet:
tc_ = lexeme[int_[phx::ref(tm_.tm_year)=(_1-1900)]>>'-'
>>int_[phx::ref(tm_.tm_mon)=(_1-1)]>>'-'
>>int_[phx::ref(tm_.tm_mday)=_1]>>+space
>>int_[phx::ref(tm_.tm_hour)=_1]>>':'
>>int_[phx::ref(tm_.tm_min)=_1]>>':'
>>int_[phx::ref(tm_.tm_sec)=_1]] [_val = (long)mktime(&tm_)];
where tc_ is a qi rule of type: qi::rule<Iterator, long(), Skipper>, tm_ is a member variable of type struct tm.
The code compiles, but doesn't work. it seems that mktime() didn't get called at all. what am I doing wrong?
you can do it in c++ 11 vi the regex.
This will be portable and standard, if your compiler is recent enough.
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
std::regex txt_regex("([0-9]{4})[-]([0-9]{2})[-]([0-9]{2})[ ]([0-9]{2})([:])([0-9]{2})([:])([0-9]{2})");//
string strTmp;
strTmp="2010-12-15 15:25:46";
std::smatch match;
std::regex_search( strTmp, match, txt_regex );
if(regex_match(strTmp,txt_regex))
cout<<"Ok"<<endl;
else
{
cout<<"Invalid input"<<endl;
return 0;
}
if ( match.empty() )
{
std::cout << "...no more matches" << std::endl;
return 0;
}
for ( auto x : match )
{
std::cout << "found: " << x << std::endl;
}
string str = match.suffix().str();
cout <<str <<std::endl;
return 0;
}
With this you can display the different parts of the string to display and then fill the structure.
Hope it helps and open for comments as usual (if something is not clear or incomplete).
Below is my following code
#include <iostream>
#include <stdlib.h>
#include <boost/regex.hpp>
#include <string>
using namespace std;
using namespace boost;
int main() {
std::string s = "Hello my name is bob";
boost::regex re("name");
boost::cmatch matches;
try{
// if (boost::regex_match(s.begin(), s.end(), re))
if (boost::regex_match(s.c_str(), matches, re)){
cout << matches.size();
// matches[0] contains the original string. matches[n]
// contains a sub_match object for each matching
// subexpression
for (int i = 1; i < matches.size(); i++){
// sub_match::first and sub_match::second are iterators that
// refer to the first and one past the last chars of the
// matching subexpression
string match(matches[i].first, matches[i].second);
cout << "\tmatches[" << i << "] = " << match << endl;
}
}
else{
cout << "No Matches(" << matches.size() << ")\n";
}
}
catch (boost::regex_error& e){
cout << "Error: " << e.what() << "\n";
}
}
It always outputs with no Matches.
Im sure that regex should work.
I used this example
http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html?page=3
from boost regex:
Important
Note that the result is true only if the expression matches the whole of the input sequence. If you want to search for an expression somewhere within the sequence then use regex_search. If you want to match a prefix of the character string then use regex_search with the flag match_continuous set.
Try boost::regex re("(.*)name(.*)"); if you want to use the expression with regex_match.