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
std::string m_Expression =
"(?<={|\|)(?<KEYS>[\w]*[^=
I am trying to parse a string out into a key value pair collection.
Thanks
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.
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?
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.
ASKER
Once again, Thank you for your help. Here is some additional information:
Here is a sample message I am trying to parse:
1234567890{message_informa tion={mess age_type=p roduct_ord er|timesta mp=14:09:2 6.20719}|o rder_infor mation={or der_number =281|order ={customer _name=john smith|prod uct=widget |price=99. 99|quantit y=5}}|erro r=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
Here is a sample message I am trying to parse:
1234567890{message_informa
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:
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";
}
}
ASKER
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.
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.
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.
ASKER
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::co nst_iterat or> 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
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::
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works! Thank you so much.
NP. Glad to help :)