-2

I have a string that contains the following information

[Messaging]
    Gatekeeper = "${Const|BaseURL}:${Const|PublicPort}"

[AvatarService]
    AvatarServerURI = "${Const|BaseURL}:${Const|PrivatePort}"

How do i look in the string for gatekeeper and out put the value of gatekeeper?

so ex looking in string for gatekeeper should produce result of ${Const|BaseURL}:${Const|PrivatePort} with no quotes.

1 Answers1

1

You can use this to extract the settings :

var settings = Regex.Split(settings_received_from_server, @"(?=\[\w+\])")
    .ToDictionary(
        section => Regex.Match(section, @"\[(?<section>\w+)\]").Groups["section"].Value,
        section => section.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
            // remove empty line or section header
            .Where(line => !string.IsNullOrWhiteSpace(line) && !Regex.IsMatch(line, @"\[.+\]"))
            // see note below
            .Select(line => Regex.Match(line, @"(?<key>\w+)\s*=\s*(?<value>.+)"))
            .ToDictionary(
                match => match.Groups["key"].Value,
                match => match.Groups["value"].Value));

// to get the setting use the following : [section][key]
settings["AvatarService"]["AvatarServerURI"]

Note: By default the double-quot is kept, in case you also have unquoted value.

If you like to remove the double-quotes, use the following regex :

@"(?<key>\w+)\s*=\s*\""(?<value>.+)\"""
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44