C++ regex_replace
Ostatnio zmodyfikowano 2017-11-30 11:01
Breakermind Temat założony przez niniejszego użytkownika |
C++ regex_replace » 2017-11-28 20:09:36 Witam nie mogę skompilować tego na debianie dlaczego? string clearInvalidSpf( string spf ) { std::transform( spf.begin(), spf.end(), spf.begin(),::tolower ); const std::regex pattern( "[^a-zA-Z0-9.-_/=:~+ ]" ); std::string newtext = std::regex_replace( spf, pattern, "" ); return newtext; }
i jakiś zamiennik na ubuntu tej funkcji: std::quoted z c++14 Pozdrawiam |
|
kmlkamilek |
» 2017-11-28 20:35:30
std::string newtext = std::regex_replace( spf, pattern, "" );
|
Jak mamy ci pomóc, skoro nie wiemy co dokładnie nie pasuje kompilatorowi? |
|
Breakermind Temat założony przez niniejszego użytkownika |
» 2017-11-28 21:06:13 Inny problem (chcę pozbyć się znaków innych niż (litery cyfry . / - _ spacja : = ) niechcianych znaków): #include <iostream> #include <regex> #include <string>
using namespace std;
int main() { const std::regex pattern( "[^a-zA-Z0-9.-_/:= ]" ); std::string String = "v=spf1 a mx ipv4:1.2.3.4 include=_spf.g.com redirect:rrr.com.com ptr:zenek.com &*^34$#%$##%"; std::string newtext = std::regex_replace( String, pattern, "" ); std::cout << newtext << std::endl; return 0; }
Wynik: v = spf1 a mx ipv4: 1.2.3.4 include = _spf.g.com redirect: rrr.com.com ptr: zenek.com ^ 34
Dlaczego nie wycina znaku ^ ??? |
|
Breakermind Temat założony przez niniejszego użytkownika |
» 2017-11-29 10:55:22 No nic zamieniłem na prosta funkcję: #include <iostream> #include <regex> #include <string> #include <sstream>
using namespace std;
string clear( string str ) { stringstream ss; int i = 0; while( i < str.length() ) { int ch =( int ) str[ i ]; if(( ch >= 45 && ch <= 57 ) ||( ch >= 97 && ch <= 122 ) ||( ch >= 65 && ch <= 90 ) ||( ch == 32 ) ||( ch == 43 ) ||( ch == 58 ) ||( ch == 61 ) ||( ch == 63 ) ||( ch == 126 ) ) { ss << str[ i ]; } i++; } return ss.str(); };
int main() { std::string spf = "v=spf1 a mx ipv4:1.2.3.4 include=_spf.g.com redirect:asy-cpp.sa.hihi ptr:bzik.zx &*^$#%$##% -all ~all ?all"; cout << clear( spf ) << endl; return 0; }
i jeszcze add slash (mysql): string quoted( string str ) { stringstream ss; unsigned int i = 0; while( i < str.length() ) { int ch =( int ) str[ i ]; if(( ch == 34 ) ||( ch == 39 ) ||( ch == 96 ) ) { ss << "\\" << str[ i ]; } } return ss.str(); }
Bye. |
|
Breakermind Temat założony przez niniejszego użytkownika |
C++ timestamp, timestamp microseconds, datetime, datetime smtp, timestamp to datetime » 2017-11-30 11:01:38 Witam kilka przydatnych funkcji: #include <time.h> #include <ctime> #include <sys/time.h> C++ unix timestamp: unsigned int unixTimestamp() { std::time_t t = std::time( 0 ); return( unsigned int ) t; }
C++ microtime in miliseconds unsigned long microtime() { struct timeval tp; gettimeofday( & tp, NULL ); long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; return ms; }
C++ current datetime, format is YYYY-MM-DD.HH:mm:ss std::string currentDateTime() { time_t now = time( 0 ); struct tm tstruct; char buf[ 80 ]; tstruct = * localtime( & now ); strftime( buf, sizeof( buf ), "%Y-%m-%d %X", & tstruct ); return buf; }
C++ datetime smtp date char * currentDateTimeSMTP() { time_t now = time( 0 ); char * dt = ctime( & now ); dt[ strlen( dt ) - 1 ] = '\0'; return dt; }
string DateTime() { time_t now = time( NULL ); string ct = std::string( ctime( & now ) ); ct[ strlen( ct.c_str() ) - 1 ] = '\0'; return ct; }
string Date( bool utc ) { time_t now = time( 0 ); char * dt = ctime( & now ); if( utc ) { tm * gmtm = gmtime( & now ); dt = asctime( gmtm ); } return std::string( dt ); }
C++ split string with delimiter vector < string > splitDelimiter( string str, string delim ) { vector < string > tokens; size_t prev = 0, pos = 0; do { pos = str.find( delim, prev ); if( pos == string::npos ) pos = str.length(); string token = str.substr( prev, pos - prev ); if( !token.empty() ) tokens.push_back( token ); prev = pos + delim.length(); } while( pos < str.length() && prev < str.length() ); return tokens; }
C++ replace all string string replaceAll( string s, string search, string replace ) { for( size_t pos = 0;; pos += replace.length() ) { pos = s.find( search, pos ); if( pos == string::npos ) break; s.erase( pos, search.length() ); s.insert( pos, replace ); } return s; }
C++ if string contain bool Contain( std::string str, std::string search ) { std::size_t found = str.find( search ); if( found != std::string::npos ) { return 1; } return 0; }
Jeżeli ktoś widzi jakieś błędy to śmiało pisać ;) |
|
« 1 » |