I want to check if a string ends with .h5 and tried the c++ regex class. But for any Input the regex_search function returns false. Other examples in the internet looks similar to my code below, so I do not understand whats going wrong.
What is wrong with my code? Thanks for any help.
#include <iostream>
#include <regex>
#include <string>
int main(int argc,char *argv[]){
std::string text = argv[1];
std::regex rx(".*\\.h5$");
bool found = std::regex_search(text.c_str(),rx);
std::cout << text << std::endl;
std::cout << "res: " << found << std::endl;
}
What about using just substr ?
#include <string>
int main (int argc, char* argv[]) {
std::string filename(argv[1]);
std::string last = filename.substr(filename.length() - 3);
return last == ".h5";
}
Replace regex_search with regex_match
std::regex rx(".*\\.h5$");
bool found = std::regex_match(argv[1], rx);
std::cout << "Result: " << std::boolalpha << found << std::endl;
Related
I want to use strings to input the path of files:
char** argv;
char* mytarget[2]={ (char*)"D:\\testlas\\BigOne.pcd",(char*)"D:\\testlas\\SmallOne.pcd" };
argv = mytarget;
for(int i=0;i<2;i++)
{
std::cout << "m.name: " << argv[i] <<std::endl;
}
However, cout outputs:
m.name: ?D:\\testlas\\BigOne.pcd
m.name: ?D:\\testlas\\SmallOne.pcd
Why is there a ? before the strings?
I use VS2017 C++11.
I created a new program and used the code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
std::string test = "abc789";
cout << test << endl;
return 0;
}
It also outputs "?abc789". Why?
std::string test = "abc789";
There is a hidden LEFT-TO-RIGHT EMBEDDING character between the opening quote " and the first letter a (Unicode character U+202A, or UTF-8 E2 80 AA). Remove it, for example by deleting and retyping the line, then the ? will go away.
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'm beginning with c++ and I don't understand how to solve this issue :
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[]) {
//std::cout << "Hello, World!\n";
string str ("Teststring");
cout << str.end() << endl;//HERE
Thanks
str.end() will give unexpected behaviour - it returns an iterator starting at the character after the end of the string.
Try replacing the last line with std::cout << str.c_str() << endl; and you'll be making progress. See http://www.cplusplus.com/reference/string/string/c_str/ for more. The pointer/iterator is actually unnecessary, so std::cout << str << endl; will work just as well.
Actually, I try to find a regular expression in a multilines string, but I think I'm on the wrong way to find the next regular expression after a new line (equals to a '\n'). Here is my regex :
#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>
#define USE ".*[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}.*(?:\\n)*"
int main(int argc, char **argv)
{
std::stringstream stream;
std::filebuf *buffer;
std::fstream fs;
std::string str;
std::regex regex(USE);
if (argc != 2)
{
std::cerr << "Think to the use !" << std::endl;
return (-1);
}
fs.open(argv[1]);
if (fs)
{
stream << (buffer = fs.rdbuf());
str = stream.str();
if (std::regex_match(str, regex))
std::cout << "Yes" << std::endl;
else
std::cout << "No" << std::endl;
fs.close();
}
return (0);
}
There are some flags that can be specified when constructing regex object, see
documentation http://en.cppreference.com/w/cpp/regex/basic_regex for details.
Short working example with regex::extended flag, where newline character '\n' is specified in search follows:
#include <iostream>
#include <regex>
int main(int argc, char **argv)
{
std::string str = "Hello, world! \n This is new line 2 \n and last one 3.";
std::string regex = ".*2.*\n.*3.*";
std::regex reg(regex, std::regex::extended);
std::cout << "Input: " << str << std::endl;
if(std::regex_match(str, reg))
std::cout << "Match" << std::endl;
else
std::cout << "NOT match" << std::endl;
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