I'm trying to write a string parser that uses the standard library methods in C++. I want to parse out of an incoming string substrings that end with a newline or a ';'. I keep getting exceptions from the regex object that I create. My pattern is:
string pattern = "(.+[\\n\\r;])";
regex cmd_sep(pattern);
I've tried it with and without the regex_constants::extended or basic flags.
You'd better post your error message, if you are using boost library. It is possible you've missed boost::regex tag.
Try this
#include <boost/regex.hpp>
#include <string>
using namespace std;
int main ()
{
string pattern = "(.+[\\n\\r;])";
static const boost::regex cmd_sep(pattern);
return 0;
}
Related
I want to match a rule, some name occur like ABA, And A must not equal B.
for example,
'allen_bob_allen' is valid.
'allen_allen_allen' is not valid.
at https://regexr.com/, I wrote the regex pattern
([a-z]+)_(?!\1)([a-z]+)_\1 and it work perfectly,
But When I write this in C++, use std::regex,
using namespace std;
#include <regex>
int main() {
std::regex regex_test2("([a-z]+)_(?<!\\1)([a-z]+)_\\1");
return 0;
}
I got the following error:
libc++abi.dylib: terminating with uncaught exception of type std::__1::regex_error: The expression contained an invalid back reference.
here is my compiler info:
Is there anyway to fix this?
This below function will produce output as {"a","b","c"} as vector for the string input "a=b+c" when delimiter is "+="
void Split( const std::string &str, std::vector<std::string> & tokens , std::string delim)
{
boost::split(tokens, str, boost::is_any_of(delim));
}
I need output as {"a=b+c"}
Please suggest for modifications or any suitable functions available
This is the code you're looking for, regular split only takes chars so you have to use split_regex instead:
#include <string>
#include <iostream>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <vector>
int main() {
std::string str = "a=b+c" ;
std::vector<std::string> result;
boost::algorithm::split_regex(result, str, boost::regex("\\+="));
}
Note you'll have to link it to boost_regex with -lboost_regex otherwise it'll fire errors at you.
Also in the string "\\+=" the reason why there are two backslashes is that regular expressions use '+' as a special character so then you have to use a '\' character to escape that usage, but one '\' turns it into a special character '\+' so you must escape the first '\' like so: '\\+='
I don't manage to use back references in regular expression in c++.
After trying more esoteric things, I tried this simple script on gcc 4.8.1:
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
regex e("(..)\\1");
string s("aaaa");
if (regex_match(s,e))
cout << "match" << endl;
return 0;
}
but it produces a runtime error. I tried various flags in regex_constants like ECMAScript or grep but to no avail. What's wrong with this way of using back references in C++ regex engine?
Just to make sure I was not missing something trivial, I tried this in Java
class TestIt
{
public static void main (String[] args) throws java.lang.Exception
{
final String s = "aaaa";
final String e = "(..)\\1";
if (s.matches(e))
System.out.printf("match");
}
};
and obviously it prints match as expected, which is reassuring.
The regex engine included in gcc (in libstdc++) is not fully working yet. This regex works as expected on clang. So this issue has nothing to do with the way C++ treats regular expressions; rather it depends on the compiler used.
A .csv file is written like this:
M9005U00-X30A0S00-1;BAS;X;-0.002;-0.095
S707RY00-X30AOS00-1;HMV;X;+0.002;+0.081
W3005U00-X30BOJ00-1;BAS;X;+0.026;-0.138
H307QZ00-X30BOJ00-1;HMV;X;-0.025;+0.122
....
now I want to create a function, i.e.
double find_and_extract (string sss)
when this function is used with a keyword as its parameter, for example
find_and_extract (W3005U00-X30BOJ00-1);
it will search in the .csv file line by line, find corresponding line (in this case it should be the third line), and extract the certin part "+0.026" in this line, return as a double.
How should I write this function?
edit: Here is the code i've written so far:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <stdlib.h>
using namespace std;
void main()
{
find_and_extract (W3005U00-X30BOJ00-1);
}
double find_and_extract (string sss)
{
vector<string> vecarray;
ifstream infile("C:\\Data\\testdata.csv");
string temppo;
string contnt;
char csv_extract[40];
stringstream ss;
vector <string>::iterator ptr;
while (!infile.eof())
{
infile.getline(csv_extract,40);
ss << csv_extract;
ss >> contnt;
vecarray.push_back(contnt);
}
for (ptr=vecarray.begin();ptr!=vecarray.end();ptr++)
{
if ((*ptr).find(sss)==0)
temppo = (*ptr).substr(27,6);
}
return (strtod(temppo.c_str(),NULL,0));
}
Could anyone help me out to point out the errors?
Seeing as you already have the file as a string, I'd use the Knuth–Morris–Pratt algorithm to find the key, find the position of the 3rd and 4th semi-colons on that line and return the string in between them.
That's just an outline - you'll need to add error handling.
Check out strtok(). This is actually a pretty trivial task, and should be a good learning project if you are still new to C++.
You could use sed: This way, you could search for the key very efficiently, without having to implement an algorithm yourself. When you found the key, you can let sed output the parts of the line you need (use regular expressions to describe the pattern and groupings to print only part of it). After that, it's a simple string to float conversion that can be done in a programming language of your choice.
For starters:
sed -n 's/RegexToMatchYourKeyAndValues/MatchedValues/p'
If the text lines in the file are the same length, you may want to read the lines as blocks (i.e. many lines == 1 block) into a buffer, then search the buffer.
Your performance bottleneck will the reading the data from the file. In general, the search method you choose will be faster than reading in the data.
Need help figuring out how to extract text from context (Honda from str), need something analogous to Perl regex
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str;
str = "<make>Honda</make>";
//Code to extract Honda from above string
cout<<str<<endl;
cin.get();
return 0;
}
need something analogous to Perl regex
Is this a trick question? :) That "something" is PCRE: "Perl-Compatible Regular Expressions".
What you really need is libxml2, and the XPath query //meta/text().
In C# (I don't know programming in C#), I know there is Regex but in C++ it may be included in external libraries