<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mittels Regular-Expression nur Nummern extrahieren]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich möchte mittels Regular-Expression mir alle Nummer zurückgeben lassen, welche in einen String sind. (Ich nutze dazu die Boost-Library)</p>
<p>Es will einfach nicht klappen.<br />
Hier mein Code, mit dem ich es derzeit versuche:</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;iostream&gt;
#include &lt;boost/regex.hpp&gt;

using namespace std;

int main(int argc, char **argv) {

	boost::regex pattern;
	boost::cmatch matches;

	//char* strPattern = &quot;(.*)([0-9]+)(.*)&quot;;
	char* strPattern = &quot;([0-9]+)&quot;;
	char* strText    = &quot;Dies hier 123456789 ist eine Zahl! Und diese 987654321 auch.&quot;;

	pattern = strPattern;

	if (boost::regex_match(strText, matches, pattern))
	{
		for (int i = 1; i &lt; matches.size(); i++)
		{
			//cout &lt;&lt; &quot;matches[i].first : &quot; &lt;&lt; matches[i].first &lt;&lt; endl;
			//cout &lt;&lt; &quot;matches[i].second: &quot; &lt;&lt; matches[i].second &lt;&lt; endl;
			string match(matches[i].first, matches[i].second);
			cout &lt;&lt; &quot;\tmatches[&quot; &lt;&lt; i &lt;&lt; &quot;] = &quot; &lt;&lt; match &lt;&lt; endl;
		}
	}
    return 0;
}
</code></pre>
<p>Als Ausgabe sollte folgendes erscheinen:<br />
**<br />
machtes[1] = 123456789<br />
machtes[2] = 987654321<br />
**</p>
<p>Was mache ich falsch?<br />
Ich habe mir ein Tool Namens &quot;Rad Software Regular Expression Designer&quot; installiert, mit welchem ich mir super das gewüschte Ergebniss ansehen kann.</p>
<p>Es ruf um HILFE ein verzweifelter C++ Anfänger.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/218220/mittels-regular-expression-nur-nummern-extrahieren</link><generator>RSS for Node</generator><lastBuildDate>Fri, 09 Oct 2026 12:32:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/218220.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 17 Jul 2008 14:06:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Mittels Regular-Expression nur Nummern extrahieren on Thu, 17 Jul 2008 14:06:02 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich möchte mittels Regular-Expression mir alle Nummer zurückgeben lassen, welche in einen String sind. (Ich nutze dazu die Boost-Library)</p>
<p>Es will einfach nicht klappen.<br />
Hier mein Code, mit dem ich es derzeit versuche:</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;iostream&gt;
#include &lt;boost/regex.hpp&gt;

using namespace std;

int main(int argc, char **argv) {

	boost::regex pattern;
	boost::cmatch matches;

	//char* strPattern = &quot;(.*)([0-9]+)(.*)&quot;;
	char* strPattern = &quot;([0-9]+)&quot;;
	char* strText    = &quot;Dies hier 123456789 ist eine Zahl! Und diese 987654321 auch.&quot;;

	pattern = strPattern;

	if (boost::regex_match(strText, matches, pattern))
	{
		for (int i = 1; i &lt; matches.size(); i++)
		{
			//cout &lt;&lt; &quot;matches[i].first : &quot; &lt;&lt; matches[i].first &lt;&lt; endl;
			//cout &lt;&lt; &quot;matches[i].second: &quot; &lt;&lt; matches[i].second &lt;&lt; endl;
			string match(matches[i].first, matches[i].second);
			cout &lt;&lt; &quot;\tmatches[&quot; &lt;&lt; i &lt;&lt; &quot;] = &quot; &lt;&lt; match &lt;&lt; endl;
		}
	}
    return 0;
}
</code></pre>
<p>Als Ausgabe sollte folgendes erscheinen:<br />
**<br />
machtes[1] = 123456789<br />
machtes[2] = 987654321<br />
**</p>
<p>Was mache ich falsch?<br />
Ich habe mir ein Tool Namens &quot;Rad Software Regular Expression Designer&quot; installiert, mit welchem ich mir super das gewüschte Ergebniss ansehen kann.</p>
<p>Es ruf um HILFE ein verzweifelter C++ Anfänger.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1548868</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1548868</guid><dc:creator><![CDATA[okin76]]></dc:creator><pubDate>Thu, 17 Jul 2008 14:06:02 GMT</pubDate></item><item><title><![CDATA[Reply to Mittels Regular-Expression nur Nummern extrahieren on Thu, 17 Jul 2008 14:32:50 GMT]]></title><description><![CDATA[<p>wenn man etwas in einem String sucht muss man regex_search benutzen. Und das Ergebnis liefert Dir auch immer nur einen (!) Ausdruck zurück; folglich muss der regex_search mehrfach aufgerufen werden, wenn man beliebig viele Treffer haben will.<br />
So geht's:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;boost/regex.hpp&gt;

using namespace std;

int main()
{
    char* strText = &quot;Dies hier 123456789 ist eine Zahl! Und diese 987654321 auch.&quot;;

    boost::regex pattern( &quot;([0-9]+)&quot; );
    boost::cmatch matches;
    for( const char* i = strText; boost::regex_search( i, matches, pattern )
        ; i = matches[0].second )
    {
        for( int i = 1; i &lt; int(matches.size()); ++i )
        {
            string match(matches[i].first, matches[i].second);
            cout &lt;&lt; &quot;\tmatches[&quot; &lt;&lt; i &lt;&lt; &quot;] = &quot; &lt;&lt; match &lt;&lt; endl;
        }
    }
    return 0;
}
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1548881</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1548881</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Thu, 17 Jul 2008 14:32:50 GMT</pubDate></item><item><title><![CDATA[Reply to Mittels Regular-Expression nur Nummern extrahieren on Thu, 17 Jul 2008 15:06:37 GMT]]></title><description><![CDATA[<p>Danke dir Werner.<br />
Das ist genau was ich wollte.<br />
Wenne mich mal siehst.... einfach anhauen... dafür gebe ich einen aus. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Gruß<br />
Niko</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1548901</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1548901</guid><dc:creator><![CDATA[okin76]]></dc:creator><pubDate>Thu, 17 Jul 2008 15:06:37 GMT</pubDate></item><item><title><![CDATA[Reply to Mittels Regular-Expression nur Nummern extrahieren on Thu, 17 Jul 2008 18:02:09 GMT]]></title><description><![CDATA[<p>Darf ich vielleicht eine etwas bisschen einfachere Lösung angeben? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<pre><code class="language-cpp">#include &lt;boost/regex.hpp&gt;

#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

void regex_callback(boost::smatch const&amp; rMatch)
{
	std::cout &lt;&lt; rMatch.str() &lt;&lt; std::endl;
}

int main()
{
    boost::regex Pattern(&quot;([0-9]+)&quot;);
    std::string Text = &quot;Dies hier 123456789 ist eine Zahl! Und diese 987654321 auch.&quot;;

    boost::sregex_iterator Iter(Text.begin(), Text.end(), Pattern);
    boost::sregex_iterator End;

    std::for_each(Iter, End, &amp;regex_callback);

    return 0;
}
</code></pre>
<p>Anstatt der Callback-Funktion kann man natürlich auch einen Funktor oder ein Boost.Bind oder sonst was verwenden.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1549004</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1549004</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Thu, 17 Jul 2008 18:02:09 GMT</pubDate></item></channel></rss>