Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7662157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:43:46+00:00 2026-05-31T13:43:46+00:00

I would like to pass the variable from a function to the main scope

  • 0

I would like to pass the variable from a function to the main scope which I’m calling, I’m trying to do like I use to do in C but it returns nothing.

I want to be able to output and deal with it after the return of the function

#include "StdAfx.h"
#include <regex>
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

std::tr1::match_results<std::string::const_iterator> match(std::string& regex, const std::string& ip,std::tr1::match_results<std::string::const_iterator> res)
{
   const std::tr1::regex pattern(regex.c_str());
   bool valid = std::tr1::regex_match(ip, res, pattern);
   std::cout << ip << " \t: " << (valid ? "valid" : "invalid") << std::endl;
   cout << "FIRST RES FOUND: " << res[1] << endl;  
   return res;
}

int main()
{

   string regex = "(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})";
   string ip = "49:22:33:444";
   std::tr1::match_results<std::string::const_iterator> res;
   match(regex,ip.c_str(), res);

   cout << "Result >" << res[1] << "< " << endl;


   _getch(); return 0;
}

When I compile and run, The output is: “FIRST RES FOUND: 49
Result ><“

It’s probably a really simple solution but what do I have to do to set it for my main can read it correctly as in: “Result >49<“

Thanks in advance. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:43:47+00:00Added an answer on May 31, 2026 at 1:43 pm

    Option 1: Use references:

    void match(string& regex, const string& ip, tr1::match_results<string::const_iterator> & res)
    {
       const tr1::regex pattern(regex.c_str());
       bool valid = tr1::regex_match(ip, res, pattern);
       cout << ip << " \t: " << (valid ? "valid" : "invalid") << endl;
       cout << "FIRST RES FOUND: " << res[1] << endl;
    }
    

    Option 2: Return the result by value and store it:

    tr1::match_results<string::const_iterator> match(string& regex, const string& ip)
    {
        tr1::match_results<string::const_iterator> res;
        // ...
        return res;
    }
    
    int main()
    {
        // ...
        tr1::match_results<string::const_iterator> res = match(regex, ip);
    }
    

    On a separate note, there should be absolutely no need for all the c_str() calls, as <regex> has a perfectly functional std::string interface. Check the documentation for details, you just have to get a couple of typenames right.


    Edit: Here are some basic examples on using std::string. There are equivalent constructions for std::wstring, char* and wchar_t*, but std::strings should be the most useful one.

    Since <regex> support is still patchy, you should consider the TR1 and Boost alternatives, too; I provide all three and you can pick one:

    namespace ns = std;          // for <regex>
    namespace ns = std::tr1;     // for <tr1/regex>
    namespace ns = boost;        // for <boost/regex.hpp>
    
    ns::regex r("");
    ns::smatch rxres;            // 's' for 'string'
    
    std::string data = argv[1];  // the data to be matched
    
    // Fun #1: Search once
    
    if (!ns::regex_search(data, rxres, r))
    {
        std::cout << "No match." << std::endl;
        return 0;
    }
    
    
    // Fun #2:  Iterate over all matches
    
    ns::sregex_iterator rt(data.begin(), data.end(), r), rend;
    
    for ( ; rt != rend; ++rt)
    {
        // *rt is the entire match object
    
        for (auto it = rt->begin(), end = rt->end(); it != end; ++it)
        {
            // *it is the current capture group; the first one is the entire match
    
            std::cout << "   Match[" << std::distance(rt->begin(), it) << "]: " << *it << ", length " << it->length() << std::endl;
        }
    }
    

    Don’t forget to handle exceptions of type ns::regex_error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to pass a variable from a function to a class. Example
I would like to pass a variable from one Controller Action to another and
I am trying to use the SetParametersInfo function to change wallpapers.I would like to
I would like to pass to a jQuery function a regular function, instead of
I would like to create a function that returns a concatinated string of a
I have a function that takes a ref parameter and would like to use
I would like to pass an argument(s) to a method being defined using define_method,
I would like to pass an x amount of geo-locations to the Google Maps
I would like to pass a reference of a method into another method and
I would like to pass a MYSQL query via Coldfusion the following date: 03/13/2010

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.