Teilstring aus Stream lesen



  • Hallo,

    ich habe einen ifstream aus dem ich gerne bestimmte Textstellen herausfiltern möchte. Wie aber geht dies?Idealerweise wäre es, wenn ich noch einem bestimmen Wort suchen könnte und dann dort die nachfolgende Zahl zurückgeliefert bekomme.

    Wie geht dies? kann mir jemand ein Beispiel geben?

    Vielen Dank!



  • Keiner?



  • man kann das leicht mit regexp oder Finite state machine realizieren

    //STL
    #include <fstream>
    #include <string>
    
    //BOOST
    #include <boost/regex.hpp>
    
    bool getMyVariableValue(const std::string &line, std::string &value);
    
    int main()
    {
    	try
    	{
    		//in string
    		std::string v;
    		std::string l = "qwre qweqwe MyVariable = 'MyValue'sdsd sd ";
    		if(getMyVariableValue(l, v))
    		{
    			std::cout << "gefunden !!! -> MyVariable = '";
    			std::cout << v << "'\n";
    		}
    
    		//in stream
    		std::string line;
    		std::ifstream is("...");		
    		while(getline(is, line))
    		{
    			std::string value;
    			if(getMyVariableValue(line, value))
    			{
    				std::cout << "gefunden !!! -> MyVariable = '";
    				std::cout << value << "'\n";
    			}			
    		}
    	}
    	catch (const std::exception &exc)
    	{
    		std::cerr << "Error : " << exc.what();
    	}		
    
    	return 0;
    } 
    
    bool getMyVariableValue(const std::string &line, std::string &value)
    {
    	/*static */boost::regex re("MyVariable\\s*=\\s*'(.*?)'");
    
    	boost::smatch what;
    	if(boost::regex_search(line, what, re))
    	{
    		if(what[1].matched)
    		{
    			value = what[1];
    			return true;
    		}		
    	}
    	return false;
    }
    


  • Hey, super!

    Vielen, vielen Dank! Hab mir sehr weitergeholfen!!!!


Anmelden zum Antworten