StringBetween bei mehreren Möglichkeiten



  • guten tag.
    Im Moment benutze ich folgende Funktion um Cookies zu extrahieren.

    string StringBetween(const string& input, const string& left, const string& right)
    {
        size_t posLeft, posRight;
    
        posLeft = input.find(left);
        posRight = input.find(right);
    
        if (posLeft == string::npos || posRight == string::npos)
        {
            return ""; // Nicht gefunden
        }
    
        posLeft += left.length();
        posRight += right.length();
        string ergebnis1 = input.substr(posLeft, input.length() - right.length() - left.length());
        int formel = (input.find(right) - posLeft);
        string ergebnis2 = ergebnis1.substr(0, formel);
        return ergebnis2;
    }
    

    Jetzt habe ich aber folgendes Problem...
    Ich muss mehrere Cookies exrahieren, wobei es mehrere Möglichkeiten gibt...
    Hier is der Code aus dem ich sie herausextrahieren will.

    HTTP/1.1 302 Found
    
    Date: Sun, 17 Jan 2010 11:55:30 GMT
    
    Server: Apache/2.2.10 (Unix) PHP/5.2.8 mod_python/3.3.2-dev-20080819 Python/2.4.4
    
    node: www11
    
    X-Database: gde
    
    Cache-Control: private, no-store, no-cache, must-revalidate
    
    Pragma: no-cache
    
    Cache-Control: no-cache="set-cookie"
    
    Set-Cookie: spcheck=1; path=/
    
    Set-Cookie: spgdex1261708=x69e969b4-NjM4Ng-9330-61-NTYxNg; path=/
    
    Set-Cookie: gsp=5053059; path=/; expires=Mon, 17-Jan-2011 11:55:30 GMT
    
    location: /main.html
    
    Vary: Accept-Encoding
    
    Content-Length: 0
    
    Connection: close
    
    Content-Type: text/html; charset=utf-8
    

    hat jemand ne idee wie ich das lösen könnte??? 😕
    mfg
    @night@



  • Öhm und wo genau liegt jetzt das Problem?



  • Das problem ist wie folgt:
    Wenn ich die Funktion wie folgt aufrufe:

    string spgdex = StringBetween(source, "Set-Cookie: spgdex1261708=", "; path=/");
    

    dann gibt er mir immer den gesamten teil zwischen dem cookie und dem anderen cookieblock mit...
    ich kanns schlecht erklären...
    dabei möchte ich aber nur den Teil von jedem Cookie...
    spgdex1261708=x69e969b4-NjM4Ng-9330-61-NTYxNg
    den Rest brauche ich gar nicht

    mfg
    @night@



  • Ich verstehe nicht so ganz was du da machst, aber ich würde es folgendermaßen machen (je nachdem wie du es brauchst, kannst du dir die fnc ja umbauen):

    #include <iostream> 
    #include <string>
    
    const std::string GetCookieData(const std::string Cookie)
    {
    	const std::size_t found_pos(Cookie.find("spgdex"));
    	std::string ReturnString;
    
    	if(found_pos != std::string::npos)
    		for(std::size_t i(found_pos + 6); // std::string("spgdex")::size() == 6
    			Cookie[i] != ';';
    			++i)
    				ReturnString.push_back(Cookie[i]);
    
    	return ReturnString;
    }
    
    int main() 
    {
    	const std::string Cookie = GetCookieData("Set-Cookie: spgdex1261708=x69e969b4-NjM4Ng-9330-61-NTYxNg; path=/ ");
    
    	Cookie;
    
    	std::cin.get();
    }
    


  • danke dir 👍
    dass hast du extrem gut gelöst! Auf so etwas wär ich nie selber gekommen 👍
    mfg
    @night@


Anmelden zum Antworten