I’m not really that good with regex, but I understand the basics. I’m trying to figure out how to do a conditional replace based upon a certain value in the match. For example:
Suppose I have some nested string structure that look like this:
"[id value]"//id and value are space delimited. id will never have spaces
id is some string id that names the [] item and value is another nested [id value] item. Its possible for value to be empty, but I’m not worried about that for now.
If I have something like this:
A) "[vehicle [toyota camry]]"
or
B) "[animal [dog rufus]]"
I’d like to be able to call a certain function (ToString() for example) based upon id that gets output as the regex.Replace is executed from the inner most [] structure.
Going from example A pseudo code:
string Return = "{0}";
var 1stValueComboID = GetInteriorValue/IDFrom("[vehicle [toyota camry]]");
//1stValueComboID.ToString() = "Company: Toyota, Make: Camry"
Return = Format.String(Return,1stValueIDCombo.ToString());
var 2stValueComboID = GetSecondValue/IDFrom("[vehicle [toyota camry]]");
//2stValueComboID.ToString() = "Type: Vehicle, {0}"
Return = Format.String(Return,2ndValueIDCombo.ToString());
This sample obviously has nothing to do with regex, but it hopefully illustrates kind of what I’m trying to do.
JoshD correctly points out that this grammar you’ve proposed (having matching pairs of brackets) cannot be parsed using a regular expression. You need to construct a custom parser with recursive descent behavior.