1

I am trying to do what should be a simple pattern match and replace:

Regex.Replace(sUserSettings, @"Name={could_be_anything};", "Name=Tim;");

I have been try along the lines of:

Regex.Replace(sUserSettings, @"Name=\*[];", "Name=Tim;");

No joy - where am I going wrong?

2 Answers 2

1

[] matches nothing.

To match anything (ungreedily), use .*?:

Regex.Replace(sUserSettings, @"Name=\*.*+?;", "Name=Tim;");

Now, I'm not sure why you need to match an asterisk first (\*). If it doesn't matter, you can leave it out:

Regex.Replace(sUserSettings, @"Name=.*?;", "Name=Tim;");
Sign up to request clarification or add additional context in comments.

2 Comments

Why do you have the \* in there? That matches a literal *.
I think that was part of his original attempt at .+? behavior. Judging by his sample input, anyway.
1
Regex.Replace( sUserSettings, @"(^|;)Name=[^;]*(;)?", "$1Name=Tim$2" )

This allows you to replace it even if it's at the beginning or end end of the string without a trailing ;.

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.