M
Martin Robins
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being parsed can completely wreck it.
The string I am trying to parse is as follows:
commandText=insert into [Trace] (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category
I am looking to retrive name value pairs where the name is the value before the equals and the value is everything after the equals, with each pair being delimited by the semi-colon ...
Name Value
commandText "insert into [Trace] (Text) values (@message + N': ' + @category)"
commandType StoredProcedure
message @message
category @category
The regular expression code is as follows:
Regex regex = new Regex(@"(?<name>[^=]*)=(?<value>[^(?:;|$)]*)(?:;|$)", RegexOptions.ExplicitCapture);
foreach (Match match in regex.Matches(initializeData)) {
string name = match.Groups[@"name"].Value.Trim().ToLower(), value = match.Groups[@"value"].Value.Trim();
switch (name) {
...
}
}
The first match found reveals a name of "insert into ..." through to "commandType". It then matches on message=@message and category=@category.
If I change the string being parsed to:
commandText=insertTrace;commandType=StoredProcedure; message=@message; category=@category
It is parsed correctly, picking up the 4 name/value pairs.
I know the problem lies in the regular expression I am using; I need to cope with the brackets and the colon.
Can anybody offer me any further help with the expression?
Cheers.
The string I am trying to parse is as follows:
commandText=insert into [Trace] (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category
I am looking to retrive name value pairs where the name is the value before the equals and the value is everything after the equals, with each pair being delimited by the semi-colon ...
Name Value
commandText "insert into [Trace] (Text) values (@message + N': ' + @category)"
commandType StoredProcedure
message @message
category @category
The regular expression code is as follows:
Regex regex = new Regex(@"(?<name>[^=]*)=(?<value>[^(?:;|$)]*)(?:;|$)", RegexOptions.ExplicitCapture);
foreach (Match match in regex.Matches(initializeData)) {
string name = match.Groups[@"name"].Value.Trim().ToLower(), value = match.Groups[@"value"].Value.Trim();
switch (name) {
...
}
}
The first match found reveals a name of "insert into ..." through to "commandType". It then matches on message=@message and category=@category.
If I change the string being parsed to:
commandText=insertTrace;commandType=StoredProcedure; message=@message; category=@category
It is parsed correctly, picking up the 4 name/value pairs.
I know the problem lies in the regular expression I am using; I need to cope with the brackets and the colon.
Can anybody offer me any further help with the expression?
Cheers.