我无法让 gcc/libstdc++ 正则表达式工作。我能做的最好的似乎是解决你的问题是这样的:
#include <iostream>
#include <string>
using namespace std;
class unquot {
string where;
char what, quote;
public:
unquot(char _what = '"', char _quote = '\\') : what(_what), quote(_quote) {}
string str() { return where; }
friend istream& operator>>(istream& i, unquot& w) {
w.where = string();
char c = i.get();
if( !i )
return i;
if( c != w.what ) {
i.setstate(ios::failbit);
i.putback(c);
return i;
}
bool quoted = false;
for( c = i.get(); i; c = i.get() ) {
if( quoted ) {
w.where.push_back(c);
quoted = false;
} else if( c == w.quote ) {
quoted = true;
} else if( c == w.what ) {
break;
} else {
w.where.push_back(c);
}
}
return i;
}
};
class until {
string where;
char what, quote;
public:
until(char _what = '"', char _quote = '\\') : what(_what), quote(_quote) {}
string str() { return where; }
friend istream& operator>>(istream& i, until& w) {
w.where = string();
char c = i.get();
if( !i )
return i;
if( c != w.what ) {
i.setstate(ios::failbit);
i.putback(c);
return i;
}
w.where.push_back(c);
bool quoted = false;
for( c = i.get(); i; c = i.get() ) {
w.where.push_back(c);
if( quoted ) {
quoted = false;
} else if( c == w.quote ) {
quoted = true;
} else if( c == w.what ) {
break;
}
}
return i;
}
};
class word {
string where;
public:
word() {}
string str() { return where; }
friend istream& operator>>(istream& i, word& w) {
bool before = true, during = false;
w.where = string();
for( char c = i.get(); i; c = i.get() ) {
bool wordchar = isalnum(c) || (c == '_');
bool spacechar = isspace(c);
bool otherchar = !wordchar && !spacechar;
if( before ) {
if( wordchar ) {
swap(before, during);
w.where.push_back(c);
} else if( otherchar ) {
i.setstate(ios::failbit);
i.putback(c);
break;
}
} else if( during ) {
if( wordchar ) {
w.where.push_back(c);
} else if( otherchar ) {
i.putback(c);
break;
} else {
during = false;
}
} else {
if( !spacechar ) {
i.putback(c);
break;
}
}
}
return i;
}
};
class skip {
char which;
public:
skip(char _which) : which(_which) {}
friend istream& operator>>(istream& i, skip& s) {
bool before = true;
for( char c = i.get(); i; c = i.get() ) {
if( c == s.which ) {
before = false;
} else if( !isspace(c) ) {
i.putback(c);
if( before )
i.setstate(ios::failbit);
break;
}
}
return i;
}
};
int main ()
{
word w;
skip eq { '=' };
unquot q;
skip semi { ';' };
while( cin >> w >> eq >> q >> semi ) {
cout << w.str() << " {" << q.str() << "}" << endl;
}
return 0;
}
您可以使用“直到 q;” 而不是“取消引用 q;” 如果你想保留引号......