0

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 replace one by one with another string?

Thanks

2 Answers 2

5

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Great! what if i want that the matching result will be w/o the '$' or the '(' ')' chars? thanks
0

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

1 Comment

you should directly use Regex.Replace instead of your foreach loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.