Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

C++ regex_replace

Ostatnio zmodyfikowano 2017-11-30 11:01
Autor Wiadomość
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?


C/C++
string clearInvalidSpf( string spf ) {
    // str to lower case
    std::transform( spf.begin(), spf.end(), spf.begin(),::tolower );
    const std::regex pattern( "[^a-zA-Z0-9.-_/=:~+ ]" );
   
    // !!!!!! TUTAJ JAKIŚ NOTE, ERROR basic string ch_t
    std::string newtext = std::regex_replace( spf, pattern, "" );
   
    return newtext;
    // std::regex_constants::icase
    // std::string newtext = std::regex_replace( String, pattern, "X", std::regex_constants::format_first_only );
}

i jakiś zamiennik na ubuntu tej funkcji:
std::quoted  z c++14

Pozdrawiam
P-167364
kmlkamilek
» 2017-11-28 20:35:30

C/C++
// !!!!!! TUTAJ JAKIŚ NOTE, ERROR basic string ch_t
std::string newtext = std::regex_replace( spf, pattern, "" );

Jak mamy ci pomóc, skoro nie wiemy co dokładnie nie pasuje kompilatorowi?
P-167368
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):

C/C++
#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:
C/C++
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 ^
???
P-167371
Breakermind
Temat założony przez niniejszego użytkownika
» 2017-11-29 10:55:22
No nic zamieniłem na prosta funkcję:

C/C++
#include <iostream>
#include <regex>
#include <string>
#include <sstream>

using namespace std;

string clear( string str ) {
    stringstream ss;
    int i = 0;
    while( i < str.length() ) {
        // Znak ascii code
        // char a = 'a';
        // int b = (int)a;
        // Znak ascii code
        // 0-9(48-57), a-z(97-122), A-Z(65-90), space(32)
        // +(43), -(45), .(46), /(47), :(58), =(61), ?(63), ~(126)
        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 ];
            // cout << "Znak " << str[i] << " Int " << ch << endl;
        }
        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):

C/C++
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.
P-167391
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:
C/C++
unsigned int unixTimestamp() {
    std::time_t t = std::time( 0 );
    return( unsigned int ) t;
}

C++ microtime in miliseconds
C/C++
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
C/C++
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
C/C++
char * currentDateTimeSMTP() {
    // current date/time based on current system
    time_t now = time( 0 );
    // convert now to string form
    char * dt = ctime( & now );
    dt[ strlen( dt ) - 1 ] = '\0';
    return dt;
}

// Date time like smtp date
string DateTime() {
    time_t now = time( NULL );
    string ct = std::string( ctime( & now ) );
    ct[ strlen( ct.c_str() ) - 1 ] = '\0';
    return ct;
}

// Date time like smtp date localtime or utc
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
C/C++
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();
        // cout << token << endl;
    }
    while( pos < str.length() && prev < str.length() );
   
    return tokens;
}

C++ replace all string
C/C++
string replaceAll( string s, string search, string replace ) {
    for( size_t pos = 0;; pos += replace.length() ) {
        // Locate the substring to replace
        pos = s.find( search, pos );
        if( pos == string::npos ) break;
        // Replace by erasing and inserting
        s.erase( pos, search.length() );
        s.insert( pos, replace );
    }
    return s;
}

C++ if string contain
C/C++
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ć ;)
P-167413
« 1 »
  Strona 1 z 1