Nested Regex Replace in C #

I am not very good at regular expression, but I understand the basics. I am trying to figure out how to make a conditional substitution based on a specific value in a match. For instance:

Suppose I have a nested string structure that looks like this:

"[id value]"//id and value are space delimited.  id will never have spaces

idis some string identifier that names the item [], and valueis another nested item [id value]. Its possible to valuebe empty, but I'm not worried about it yet.

If I have something like this:

A) "[vehicle [toyota camry]]"
or
B) "[animal [dog rufus]]"

I would like to be able to call a specific function (for example, ToString ()) on a basis idthat is inferred as regex.Replace being executed from the innermost structure [].

Transition from the example Pseudocode:

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());

, , , , , , .

+3
2

JoshD , ( ) . .

+1

, , ,

[id1 [id2 [id3 [id4 .. value]] ... ],

. ? , , . , , , .

, ,

static Tuple<String, String> Parse(String s)
{

    var match = Regex.Match(s, @"^\[(\w*) (.*)\]$", RegexOptions.None);
    return new Tuple<String, String>(match.Groups[1].ToString(), match.Groups[2].ToString());
}

var result = Parse("[animal [dog rufus]]");
// result = {Item 1 = "animal", Item2 = "[dog rufus]" }
var inner = Parse(result.Item2);
// inner = { Item 1 = "dog", Item2 ="rufus"}

Parse , .

, , =)

+2

Source: https://habr.com/ru/post/1769066/


All Articles