Fix wxRegEx example in the documentation

Fix typos and make the code actually compile.

Closes #17475.
This commit is contained in:
Iwbnwif Yiw
2016-04-02 18:52:23 +02:00
committed by Vadim Zeitlin
parent 8baaa652ef
commit 1765794015

View File

@@ -84,29 +84,39 @@ enum
@library{wxbase} @library{wxbase}
@category{data} @category{data}
Examples: Example:
A bad example of processing some text containing email addresses (the example A (bad) example of processing some text containing email addresses (the example
is bad because the real email addresses can have more complicated form than is bad because the real email addresses can have more complicated form than
@c user@host.net): @c user@host.net):
@code @code
wxString text; wxString originalText = "This is some text with foo@example.com and bar@example.com";
...
wxRegEx reEmail = "([^@]+)@([[:alnum:].-_].)+([[:alnum:]]+)"; // Regex. to match an email address and extract its subparts.
if ( reEmail.Matches(text) ) wxRegEx reEmail("([^@ -]+)@([[:alnum:]_]+).([[:alnum:]]{2,4})");
wxString processText = originalText;
while ( reEmail.Matches(processText) )
{ {
wxString text = reEmail.GetMatch(email); // Find the size of the first match and print it.
wxString username = reEmail.GetMatch(email, 1); size_t start, len;
if ( reEmail.GetMatch(email, 3) == "com" ) // .com TLD? reEmail.GetMatch(&start, &len, 0);
{ std::cout << "Email: " << reEmail.GetMatch(processText, 0) << std::endl;
...
} // Print the submatches.
std::cout << "Name: " << reEmail.GetMatch(processText, 1) << std::endl;
std::cout << "Domain: " << reEmail.GetMatch(processText, 2) << std::endl;
std::cout << "TLD: " << reEmail.GetMatch(processText, 3) << std::endl;
// Process the remainder of the text if there is any.
processText = processText.Mid (start + len);
} }
// or we could do this to hide the email address // Or this will replace all names with "HIDDEN".
size_t count = reEmail.ReplaceAll(text, "HIDDEN@\\2\\3"); size_t count = reEmail.ReplaceAll(&originalText, "HIDDEN@\\2.\\3");
printf("text now contains %u hidden addresses", count); std::cout << "text now contains " << count << " hidden addresses" << std::endl;
std::cout << originalText << std::endl;
@endcode @endcode
*/ */
class wxRegEx class wxRegEx