Regular expressions: Difference between revisions

→‎{{header|C++}}: Edited to reflect usage of C++11's regex, but kept a Boost.Regex blurb for C++03 and earlier
(Regular expressions en FreeBASIC)
(→‎{{header|C++}}: Edited to reflect usage of C++11's regex, but kept a Boost.Regex blurb for C++03 and earlier)
Line 586:
 
=={{header|C++}}==
{{works with|g++|4.0.2}} (may need to be retested?)
Standards earlier than C++11 can make use of Boost's Regex library via boost/regex.hpp
 
{{libheader|Boost}}
<lang cpp>#include <iostream>
#include <string>
#include <iterator>
#include <boost/regex.hpp>
 
int main()
{
booststd::regex re(".* string$");
std::string s = "Hi, I am a string";
 
// match the complete string
if (booststd::regex_match(s, re))
std::cout << "The string matches.\n";
else
Line 606:
 
// match a substring
booststd::regex re2(" a.*a");
booststd::smatch match;
if (booststd::regex_search(s, match, re2))
{
std::cout << "Matched " << match.length()
Line 622:
// replace a substring
std::string dest_string;
booststd::regex_replace(std::back_inserter(dest_string),
s.begin(), s.end(),
re2,
Anonymous user