C++/CLI Code Snippet - Extract Numeric values

C++/CLI Code Snippet - Extract Numeric values

C++/CLI Code Snippet - Extract Numeric values

C++/CLI code snippet to extracts all the numeric values from a string. ExtractNumbers returns string array of numeric values successful matches by iteratively applying a regular expression pattern to the input string.

Bookmark:

C++/CLI Code Snippet - Extract Numeric values

This .Net C++/CLI code snippet extracts all the numeric values from a string. Data mining for numeric values done by set of successful matches found by iteratively applying a regular expression pattern to the input string.

array<System::String^> ^ExtractNumbers(System::String ^str)
{
    // Find matches.
    System::Text::RegularExpressions::MatchCollection ^matches = System::Text::RegularExpressions::Regex::Matches(str, "(\\d+\\.?\\d*|\\.\\d+)");

    array<System::String^> ^MatchList = gcnew array<System::String^>(matches->Count);

    // Report on each match.
    int c = 0;
    for each (System::Text::RegularExpressions::Match ^match in matches)
    {
        MatchList[c] = match->ToString();
        c++;
    }

    return MatchList;
}


C++/CLI Keywords Used:

  • Regex
  • Match
  • MatchCollection
  • Regex.Matches

Code Snippet Information:

  • Applies To: .Net, C++, CLI, Data Mining, Numeric Value Extract, Regular Expression
  • Programming Language : C++/CLI

External Resources:

Leave a comment