开发者

Parse and replace string using Regex

开发者 https://www.devze.com 2022-12-23 06:04 出处:网络
i have various strings that look like that: $(gateway.jms.jndi.ic.url,0,tibjmsnaming, tcp)/topic/$(gateway.destination.prefix)$(gateway.StatusTopicName),$(gateway.jms.jndi.ic.username),$(gateway.jms.

i have various strings that look like that:

$(gateway.jms.jndi.ic.url,0,tibjmsnaming, tcp)/topic/$(gateway.destination.prefix)$(gateway.StatusTopicName),$(gateway.jms.jndi.ic.username),$(gateway.jms.jndi.ic.password),abinding,tBinding

i'm trying to figure out a way to extract the $(...) sections and replace them with some other string.

is there anyway in C# to parse those groups and repla开发者_StackOverflow中文版ce one by one with another string?

Thanks


This regular expression will capture those sections:

\$\([^)]+\)

Then replace like this (this example changes each match to it's uppercase equivalent - you can add whatever custom logic you wish):

Regex.Replace(candidate, @"\$\([^)]+\)", delegate(Match m) {
    return m.ToString().ToUpper();
});


I am not so good with delegate.s Here is what i came up with using Andrew's regex:

string test1 = @"$(gateway.jms.jndi.ic.url,0,tibjmsnaming, tcp)/topic/$(gateway.destination.prefix)$(gateway.StatusTopicName),$(gateway.jms.jndi.ic.username),$(gateway.jms.jndi.ic.password),abinding,tBinding";

            string regex1 = @"\$\([^)]+\)";

            var matches = Regex.Matches(test1, regex1);

            Console.WriteLine(matches.Count);
            foreach (Match match in matches)
            {
                test1 = test1.Replace(match.Value, "your String");                  
            }
            Console.WriteLine(test1);
0

精彩评论

暂无评论...
验证码 换一张
取 消