forked from boostorg/regex
merged changes in regex5 branch
[SVN r26692]
This commit is contained in:
@ -1,14 +1,21 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2004
|
||||
* Copyright (c) 2003-2004
|
||||
* Dr John Maddock
|
||||
*
|
||||
* Use, modification and distribution are subject to the
|
||||
* Boost Software License, Version 1.0. (See accompanying file
|
||||
* Use, modification and distribution are subject to the
|
||||
* Boost Software License, Version 1.0. (See accompanying file
|
||||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* LOCATION: see http://www.boost.org for most recent version.
|
||||
* FILE captures_example.cpp
|
||||
* VERSION see <boost/version.hpp>
|
||||
* DESCRIPTION: Demonstrate the behaviour of captures.
|
||||
*/
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
#include <iostream>
|
||||
|
||||
|
182
example/snippets/icu_example.cpp
Normal file
182
example/snippets/icu_example.cpp
Normal file
@ -0,0 +1,182 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2004
|
||||
* Dr John Maddock
|
||||
*
|
||||
* Use, modification and distribution are subject to the
|
||||
* Boost Software License, Version 1.0. (See accompanying file
|
||||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* LOCATION: see http://www.boost.org for most recent version.
|
||||
* FILE mfc_example.cpp
|
||||
* VERSION see <boost/version.hpp>
|
||||
* DESCRIPTION: examples of using Boost.Regex with MFC and ATL string types.
|
||||
*/
|
||||
|
||||
#include <boost/regex/config.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ICU
|
||||
|
||||
#include <boost/regex/icu.hpp>
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
|
||||
//
|
||||
// Find out if *password* meets our password requirements,
|
||||
// as defined by the regular expression *requirements*.
|
||||
//
|
||||
bool is_valid_password(const UnicodeString& password, const UnicodeString& requirements)
|
||||
{
|
||||
return boost::u32regex_match(password, boost::make_u32regex(requirements));
|
||||
}
|
||||
|
||||
//
|
||||
// Extract filename part of a path from a UTF-8 encoded std::string and return the result
|
||||
// as another std::string:
|
||||
//
|
||||
std::string get_filename(const std::string& path)
|
||||
{
|
||||
boost::u32regex r = boost::make_u32regex("(?:\\A|.*\\\\)([^\\\\]+)");
|
||||
boost::smatch what;
|
||||
if(boost::u32regex_match(path, what, r))
|
||||
{
|
||||
// extract $1 as a CString:
|
||||
return what.str(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("Invalid pathname");
|
||||
}
|
||||
}
|
||||
|
||||
UnicodeString extract_greek(const UnicodeString& text)
|
||||
{
|
||||
// searches through some UTF-16 encoded text for a block encoded in Greek,
|
||||
// this expression is imperfect, but the best we can do for now - searching
|
||||
// for specific scripts is actually pretty hard to do right.
|
||||
boost::u32regex r = boost::make_u32regex(L"[\\x{370}-\\x{3FF}](?:[^[:L*:]]|[\\x{370}-\\x{3FF}])*");
|
||||
boost::u16match what;
|
||||
if(boost::u32regex_search(text, what, r))
|
||||
{
|
||||
// extract $0 as a CString:
|
||||
return UnicodeString(what[0].first, what.length(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("No Greek found!");
|
||||
}
|
||||
}
|
||||
|
||||
void enumerate_currencies(const std::string& text)
|
||||
{
|
||||
// enumerate and print all the currency symbols, along
|
||||
// with any associated numeric values:
|
||||
const char* re =
|
||||
"([[:Sc:]][[:Cf:][:Cc:][:Z*:]]*)?"
|
||||
"([[:Nd:]]+(?:[[:Po:]][[:Nd:]]+)?)?"
|
||||
"(?(1)"
|
||||
"|(?(2)"
|
||||
"[[:Cf:][:Cc:][:Z*:]]*"
|
||||
")"
|
||||
"[[:Sc:]]"
|
||||
")";
|
||||
boost::u32regex r = boost::make_u32regex(re);
|
||||
boost::u32regex_iterator<std::string::const_iterator> i(boost::make_u32regex_iterator(text, r)), j;
|
||||
while(i != j)
|
||||
{
|
||||
std::cout << (*i)[0] << std::endl;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void enumerate_currencies2(const std::string& text)
|
||||
{
|
||||
// enumerate and print all the currency symbols, along
|
||||
// with any associated numeric values:
|
||||
const char* re =
|
||||
"([[:Sc:]][[:Cf:][:Cc:][:Z*:]]*)?"
|
||||
"([[:Nd:]]+(?:[[:Po:]][[:Nd:]]+)?)?"
|
||||
"(?(1)"
|
||||
"|(?(2)"
|
||||
"[[:Cf:][:Cc:][:Z*:]]*"
|
||||
")"
|
||||
"[[:Sc:]]"
|
||||
")";
|
||||
boost::u32regex r = boost::make_u32regex(re);
|
||||
boost::u32regex_token_iterator<std::string::const_iterator>
|
||||
i(boost::make_u32regex_token_iterator(text, r, 1)), j;
|
||||
while(i != j)
|
||||
{
|
||||
std::cout << *i << std::endl;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Take a credit card number as a string of digits,
|
||||
// and reformat it as a human readable string with "-"
|
||||
// separating each group of four digit;,
|
||||
// note that we're mixing a UTF-32 regex, with a UTF-16
|
||||
// string and a UTF-8 format specifier, and it still all
|
||||
// just works:
|
||||
//
|
||||
const boost::u32regex e = boost::make_u32regex("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z");
|
||||
const char* human_format = "$1-$2-$3-$4";
|
||||
|
||||
UnicodeString human_readable_card_number(const UnicodeString& s)
|
||||
{
|
||||
return boost::u32regex_replace(s, e, human_format);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// password checks using u32regex_match:
|
||||
UnicodeString pwd = "abcDEF---";
|
||||
UnicodeString pwd_check = "(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]]).{6,}";
|
||||
bool b = is_valid_password(pwd, pwd_check);
|
||||
assert(b);
|
||||
pwd = "abcD-";
|
||||
b = is_valid_password(pwd, pwd_check);
|
||||
assert(!b);
|
||||
// filename extraction with u32regex_match:
|
||||
std::string file = "abc.hpp";
|
||||
file = get_filename(file);
|
||||
assert(file == "abc.hpp");
|
||||
file = "c:\\a\\b\\c\\d.h";
|
||||
file = get_filename(file);
|
||||
assert(file == "d.h");
|
||||
|
||||
// Greek text extraction with u32regex_search:
|
||||
UnicodeString text = L"Some where in \x0391\x039D\x0395\x0398\x0391 2004";
|
||||
UnicodeString greek = extract_greek(text);
|
||||
assert(greek == L"\x0391\x039D\x0395\x0398\x0391 2004");
|
||||
|
||||
// extract currency symbols with associated value, use iterator interface:
|
||||
std::string text2 = " $100.23 or \xC2\xA3""198.12 "; // \xC2\xA3 is the <20> sign encoded in UTF-8
|
||||
enumerate_currencies(text2);
|
||||
enumerate_currencies2(text2);
|
||||
|
||||
UnicodeString credit_card_number = "1234567887654321";
|
||||
credit_card_number = human_readable_card_number(credit_card_number);
|
||||
assert(credit_card_number == "1234-5678-8765-4321");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "<NOTE>ICU support not enabled, feature unavailable</NOTE>";
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
162
example/snippets/mfc_example.cpp
Normal file
162
example/snippets/mfc_example.cpp
Normal file
@ -0,0 +1,162 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2004
|
||||
* Dr John Maddock
|
||||
*
|
||||
* Use, modification and distribution are subject to the
|
||||
* Boost Software License, Version 1.0. (See accompanying file
|
||||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* LOCATION: see http://www.boost.org for most recent version.
|
||||
* FILE mfc_example.cpp
|
||||
* VERSION see <boost/version.hpp>
|
||||
* DESCRIPTION: examples of using Boost.Regex with MFC and ATL string types.
|
||||
*/
|
||||
|
||||
#ifdef TEST_MFC
|
||||
|
||||
#include <boost/regex/mfc.hpp>
|
||||
#include <cstringt.h>
|
||||
#include <atlstr.h>
|
||||
#include <assert.h>
|
||||
#include <tchar.h>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _UNICODE
|
||||
#define cout wcout
|
||||
#endif
|
||||
|
||||
//
|
||||
// Find out if *password* meets our password requirements,
|
||||
// as defined by the regular expression *requirements*.
|
||||
//
|
||||
bool is_valid_password(const CString& password, const CString& requirements)
|
||||
{
|
||||
return boost::regex_match(password, boost::make_regex(requirements));
|
||||
}
|
||||
|
||||
//
|
||||
// Extract filename part of a path from a CString and return the result
|
||||
// as another CString:
|
||||
//
|
||||
CString get_filename(const CString& path)
|
||||
{
|
||||
boost::tregex r(__T("(?:\\A|.*\\\\)([^\\\\]+)"));
|
||||
boost::tmatch what;
|
||||
if(boost::regex_match(path, what, r))
|
||||
{
|
||||
// extract $1 as a CString:
|
||||
return CString(what[1].first, what.length(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("Invalid pathname");
|
||||
}
|
||||
}
|
||||
|
||||
CString extract_postcode(const CString& address)
|
||||
{
|
||||
// searches throw address for a UK postcode and returns the result,
|
||||
// the expression used is by Phil A. on www.regxlib.com:
|
||||
boost::tregex r(__T("^(([A-Z]{1,2}[0-9]{1,2})|([A-Z]{1,2}[0-9][A-Z]))\\s?([0-9][A-Z]{2})$"));
|
||||
boost::tmatch what;
|
||||
if(boost::regex_search(address, what, r))
|
||||
{
|
||||
// extract $0 as a CString:
|
||||
return CString(what[0].first, what.length());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("No postcode found");
|
||||
}
|
||||
}
|
||||
|
||||
void enumerate_links(const CString& html)
|
||||
{
|
||||
// enumerate and print all the <a> links in some HTML text,
|
||||
// the expression used is by Andew Lee on www.regxlib.com:
|
||||
boost::tregex r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
|
||||
boost::tregex_iterator i(boost::make_regex_iterator(html, r)), j;
|
||||
while(i != j)
|
||||
{
|
||||
std::cout << (*i)[1] << std::endl;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void enumerate_links2(const CString& html)
|
||||
{
|
||||
// enumerate and print all the <a> links in some HTML text,
|
||||
// the expression used is by Andew Lee on www.regxlib.com:
|
||||
boost::tregex r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
|
||||
boost::tregex_token_iterator i(boost::make_regex_token_iterator(html, r, 1)), j;
|
||||
while(i != j)
|
||||
{
|
||||
std::cout << *i << std::endl;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Take a credit card number as a string of digits,
|
||||
// and reformat it as a human readable string with "-"
|
||||
// separating each group of four digits:
|
||||
//
|
||||
const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"));
|
||||
const CString human_format = __T("$1-$2-$3-$4");
|
||||
|
||||
CString human_readable_card_number(const CString& s)
|
||||
{
|
||||
return boost::regex_replace(s, e, human_format);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// password checks using regex_match:
|
||||
CString pwd = "abcDEF---";
|
||||
CString pwd_check = "(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]]).{6,}";
|
||||
bool b = is_valid_password(pwd, pwd_check);
|
||||
assert(b);
|
||||
pwd = "abcD-";
|
||||
b = is_valid_password(pwd, pwd_check);
|
||||
assert(!b);
|
||||
|
||||
// filename extraction with regex_match:
|
||||
CString file = "abc.hpp";
|
||||
file = get_filename(file);
|
||||
assert(file == "abc.hpp");
|
||||
file = "c:\\a\\b\\c\\d.h";
|
||||
file = get_filename(file);
|
||||
assert(file == "d.h");
|
||||
|
||||
// postcode extraction with regex_search:
|
||||
CString address = "Joe Bloke, 001 Somestreet, Somewhere,\nPL2 8AB";
|
||||
CString postcode = extract_postcode(address);
|
||||
assert(postcode = "PL2 8NV");
|
||||
|
||||
// html link extraction with regex_iterator:
|
||||
CString text = "<dt><a href=\"syntax_perl.html\">Perl Regular Expressions</a></dt><dt><a href=\"syntax_extended.html\">POSIX-Extended Regular Expressions</a></dt><dt><a href=\"syntax_basic.html\">POSIX-Basic Regular Expressions</a></dt>";
|
||||
enumerate_links(text);
|
||||
enumerate_links2(text);
|
||||
|
||||
CString credit_card_number = "1234567887654321";
|
||||
credit_card_number = human_readable_card_number(credit_card_number);
|
||||
assert(credit_card_number == "1234-5678-8765-4321");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "<NOTE>MFC support not enabled, feature unavailable</NOTE>";
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user