Link to home
Start Free TrialLog in
Avatar of johnthomaswarner
johnthomaswarner

asked on

How do i convert C# Regex to C++ Regex

How would I convert the following C# Regex match pattern to use in the boost C++ regex library?

std::string m_Expression =
 "(?<={|\|)(?<KEYS>[\w]*[^=}{\|]*)=(?<VALUES>[^{][\.\w-]*[^=}{\|]*)(?=(}|\|)*)";

I am trying to parse a string out into a key value pair collection.

Thanks
Avatar of kaufmed
kaufmed
Flag of United States of America image

Have you actually tried the regex via boost? I believe it supports most of the same features that the .NET engine supports. The only part that may fail, that I can see, is the use of unbounded quantifiers in the lookbehinds.
Avatar of johnthomaswarner
johnthomaswarner

ASKER

I have tried it using boost, and used the option to take it as a literal string. It returns no results from my message as opposed to using the exact same regex in .NET.

What do I need to change in the lookbehind to make it compatible with boost regex?

I haven't forgotten about you. I am going to try and install the boost libraries on my Linux box and do some testing.
Once again, Thank you for your help. Here is some additional information:

Here is a sample message I am trying to parse:

1234567890{message_information={message_type=product_order|timestamp=14:09:26.20719}|order_information={order_number=281|order={customer_name=johnsmith|product=widget|price=99.99|quantity=5}}|error=0}

As you can see above, we have a message number, followed by the message, which is enclosed in brackets. It contains items that are delimited by the bar. Items can also be another nested bar-delimited
collection in brackets, and the order can be infinite.

The idea is to parse the message into a key value pair collection.
such as:
key=johnsmith value = widget
key=order_number value = 281.

The REGEX script i have above works on .NET/C#, but not in C++. I'm assuming the boost regex follows the same rules as C++.

Thanks
Sorry it took so long. Not having the net @ home is a real pain  :)

Firstly, you have to double-up the backslashes since they are escape characters in C++. Secondly, you have to escape the curly braces which are NOT inside of bracket-expressions (i.e. not [}{] ).

The following gave me no issues:
#include <string>
#include <iostream>
#include <boost/regex.hpp>

using namespace std;
using namespace boost;

int main()
{
        string test = "1234567890{message_information={message_type=product_order|timestamp=14:09:26.20719}|order_information={order_number=281|order={customer_name=johnsmith|p$
        regex pattern("(?<=\\{|\\|)(?<KEYS>[\\w]*[^=}{\\|]*)=(?<VALUES>[^{][\\.\\w-]*[^=}{\\|]*)(?=(\\}|\\|)*)");

        if (regex_search(test, pattern))
        {
                cout << "Matched!!\n";
        }
        else
        {
                cout << "Did not match :(\n";
        }
}

Open in new window

Thank you much for your help, and sorry for the delay. I noticed that the regex search does return true,
but when I try regex_match instead, it returns false. I was trying to get a key/value pair collection
from the message. Why would it do this?

Thanks.
match tries to match the pattern to the whole string; search tries to match the pattern against a substring of the string. It's similar to Java's match and search regex methods [I don't know who came first, I just know they are similar :)  ]

If you want to ensure the whole string matches the pattern:  match.
If you want to ensure some part of the string matches the pattern:  search.
Gotcha. Now in my regex search, i want to return a collection of the results.
In C#, it is just simply a match collection with different groups for key/value in each match.
How would I go about this?

boost::match_results<std::string::const_iterator> m_Results;

Currently, I have just a flat collection for my results, and is only returning the first 2 keys/values, instead of all in the nested collection. What do I need to do to get a collection of keys/values i can iterate through?
key=string/value=string is fine.

Thanks



 
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works! Thank you so much.
NP. Glad to help  :)