String Parsing Problem...



  • Hallo,
    Ich hab nen Problem was mich echt "festnagelt" und mir keine Ruhige nacht gönnt auch wenns vielleicht ein einfaches Problem ist also ich habe diesen String aus dem IRC : :user!user2?@host PRIVMSG #channel :TEXT , man sollte also zum Schluss das hier in seperaten Variabeln haben <user> <user2> <host> <PRIVMSG> <#channel> <TEXT> . Aber hierran scheitere ich leider ich habe es so probiert :

    string username = strRecv.substr(1, strRecv.find('!') - 1);
    string chan = strRecv.substr(strRecv.find('#') , strRecv.find(" :") - strRecv.find('#'));
    

    Wobei das mit dem username funktioniert aber an den channel komme ich nicht ran ,vielleicht gibt es ja sogar noch eine einfachere Methode ?
    Hoffe ihr habt das Problem verstanden...

    Danke ist mir echt wichtig das ich das hinbekomme 😃



  • #include <string>
    #include <iostream>
    
    using std::string;
    
    char delimiters[] = {'!', '?', ' ', '#', ':'};
    const size_t size = 6;
    
    class SimpleParser
    {
    private:
    	std::pair<string::size_type, string::size_type> words[size];		
    public:
    	SimpleParser(const string &mess)
    		:	mess(mess)
    	{
    		words[0].first = 1;	
    		int word = 0;
    		string::size_type i = 1, len = mess.length();
    		while((i < len) && (word < size))
    		{
    			if(mess[i] == delimiters[word])
    			{
    				words[word].second = i - words[word].first;				
    				words[++word].first = ++i;
    			}
    			else
    			{
    				++i;
    			}			
    		}
    		words[word].second = i - words[word].first;	
    	}
    private:
    	string getSubstr(size_t n) const
    	{
    		return mess.substr(words[n].first, words[n].second);
    	}
    public:
    	string getUser() const { return getSubstr(0); }
    	string getUser2() const { return getSubstr(1); }
    	string getHost() const { return getSubstr(2); }
    	string getPrivmsg() const { return getSubstr(3); }
    	string getChannel() const { return getSubstr(4); }
    	string getText() const { return getSubstr(5); }
    
    private:
    	string mess;		
    };
    
    int main()
    {
    	SimpleParser parser(":user!user2?@host PRIVMSG #channel :TEXT ");
    
    	std::cout << parser.getUser() << std::endl;
    	std::cout << parser.getUser2() << std::endl;
    	std::cout << parser.getHost() << std::endl;
    
    	std::cout << parser.getPrivmsg() << std::endl;
    	std::cout << parser.getChannel() << std::endl;
    	std::cout << parser.getText() << std::endl;
    
    	return 0;
    }
    


  • Danke das Klappt wunderbar 👍 👍


Anmelden zum Antworten