0

I have this code in C#:

string data = "something ... 1,000 anything 20,000 other thing...";
string pattern = @"[0-9]+([\,|\.][0-9]{1,})*([\.\,][0-9]{1,})?";

MatchCollection collection = Regex.Matches(data, pattern);

foreach (Match item in collection)
{
    Console.WriteLine("{0} - {1} - {2}", item.Value, item.Index, item.Length);
}

Console.WriteLine();
Console.WriteLine("End!");
Console.ReadKey();

... and I tried to convert it in C++ (native code, without .net assemblies), so I get something like this:

void main()
    {
        string data = "something ... 1,000 anything 20,000 other thing...";
        regex pattern("([0-9]+([\\,|\\.][0-9]{1,})*([\\.\\,][0-9]{1,})?)");


        const sregex_token_iterator end;

        for (sregex_token_iterator i(data.begin(), data.end(), pattern); i != end; ++i)
        {
            std::cout << i->str() << "-" << i->length() << std::endl;
        }

        cout << endl << "End!";
        fflush(stdin); 
        getchar(); 
    }

So, how can I get the index of the match?

8
  • 2
    I don't know C#, but in C++ there is not a standard way to use regex, you need to choose a library. Usually the syntax for regex remains the same Commented Oct 17, 2011 at 11:54
  • 1
    @wiso: isn't there? ;) Commented Oct 17, 2011 at 11:55
  • @wiso Beep... In C++11 there are regexes :-) Commented Oct 17, 2011 at 11:55
  • Do you want pure regex-free C++ code, code using C++0x regex support or some code using some other regex library? Commented Oct 17, 2011 at 11:56
  • There is even the "older" boost regex library (that was "imported" in C++11) Commented Oct 17, 2011 at 11:58

2 Answers 2

5

Depending on your compiler, the <regex> header might be available, in which case you can simply rewrite the regular expression using the C++ API, which should be trivial.

If that's not available, <tr1/regex> might be available, or failing that, you can use teh Boost.Regex third party lib.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to user <tr1/regex>, but, with it, how can I get a "match" collection?
Have you looked at your compiler's documentation for the header? :)
@Eduardo - update your question retaining the c# code but explaining you're using tr1/regex, pasting in your code, and explaining what your problem is (compile error? bad results?) for best answers.
0

I solved this way:

struct MatchInfo
{
    string value;
    int index;
    int length;
};

vector<MatchInfo> DoRegex(string data, string pattern)
{
    regex patternRegex(pattern);
    sregex_token_iterator end;
    vector<MatchInfo> result;

    for (sregex_token_iterator i(data.begin(), data.end(), patternRegex); i != end; ++i)
    {
        MatchInfo item;

        item.index = i->first - data.begin();
        item.length = i->length();
        item.value = i->str();

        result.push_back(item);
    }

    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.