Я сейчас пытаюсь научиться немного C ++ с нуля.
Я хорошо разбираюсь в Python, Perl, Javascript, но только кратко столкнулся с C ++, в
обстановка в классе в прошлом. Прошу прощения за наивность моего вопроса.
Я хотел бы разбить строку, используя регулярное выражение, но мне не повезло
четкий, точный, эффективный и полный пример того, как сделать это в C ++.
В Perl это обычное действие, и поэтому оно может быть выполнено тривиальным образом,
/home/me$ cat test.txt
this is aXstringYwith, some problems
and anotherXY line with similar issues
/home/me$ cat test.txt | perl -e'
> while(<>){
> my @toks = split(/[\sXY,]+/);
> print join(" ",@toks)."\n";
> }'
this is a string with some problems
and another line with similar issues
Я хотел бы знать, как лучше всего выполнить эквивалент в C ++.
EDIT:
Я думаю, что нашел то, что искал в библиотеке наддува, как упомянуто ниже.
boost regex-token-iterator (почему не подчеркивается работа?)
Наверное, я не знал, что искать.
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
int main(int argc)
{
string s;
do{
if(argc == 1)
{
cout << "Enter text to split (or \"quit\" to exit): ";
getline(cin, s);
if(s == "quit") break;
}
else
s = "This is a string of tokens";
boost::regex re("\\s+");
boost::sregex_token_iterator i(s.begin(), s.end(), re, -1);
boost::sregex_token_iterator j;
unsigned count = 0;
while(i != j)
{
cout << *i++ << endl;
count++;
}
cout << "There were " << count << " tokens found." << endl;
}while(argc == 1);
return 0;
}