0

i am having string "Test uid=1 Test2 uid=2 Test3 uid=3"

i wrote regex to get all uid.

That gives me list of uid {1,2,3}

for this values i have some diff values {1 = 2015, 2=2016, 3=3014}

I tried to use Regex.replace(str, regexPattern, "")

As it replace all the regex matching item by single value.

Now i want to replace this old values by new value.

Code

string str = "Test uid=1 Test2 uid=2 Test3 uid=3"
var uiList = regex.Matches(str);

I am thinking about MatchCollection.

1 Answer 1

3

You can use the version of Regex.Replace accepting MatchEvaluator - Regex.Replace Method (String, String, MatchEvaluator)

Sample method (without checking of input data):

string input = "Test uid=1 Test2 uid=2 Test3 uid=3";
var replacements = new Dictionary<string,string> {
    { "1", "2015" },
    { "2", "2016" },
    { "3", "3014" }
};
string output = Regex.Replace(input, @"(?<=uid=)\d+", m => replacements[m.Value]);
Sign up to request clarification or add additional context in comments.

Comments

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.