Kendimi şu anda C++ 'dan biraz kendim öğrenmeye çalışıyorum.
Python, perl, javascript'te çok deneyimliyim ama geçmişte bir sınıf ortamında sadece C++ ile karşılaştı. Lütfen sorumun naifini affedin.C++ düzenli bir ifade kullanarak bir dizgeyi belirtin
Normal bir ifade kullanarak bir dizeyi ayırmak istiyorum ancak 'u C++ 'da nasıl yapacağınıza dair net, kesin, verimli ve tam bir örnek bulma konusunda çok şansım olmadı.
, ben en iyi C++ eşdeğer gerçekleştirmek için bilmek istiyorum
/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
Perlde bu eylem yaygındır ve dolayısıyla önemsiz bir şekilde gerçekleştirilebilir.
DÜZENLEME:
Aşağıda belirtildiği gibi destek kitaplığında aradığımı buldum.
boost regex-token-iterator (neden alt çizgiler çalışmaz?)
ben aramak bilmiyordum sanırım.
#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;
}
... söz o halde bunu buldum ve cevabı yayınladı. Birisi gelirse ve bu soruyu yararlı bulursa ... seçtikleri ile birlikte topluluğun cevabını görmek isterler. Cevabınız komünitlerin en iyi seçim olmayabilir. –