bigtv
bigtv

Reputation: 2711

Regex.Replace - HELP!

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?

Upvotes: 1

Views: 169

Answers (2)

Paul Alexander
Paul Alexander

Reputation: 32377

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

Upvotes: 1

BoltClock
BoltClock

Reputation: 724192

[] 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;");

Upvotes: 1

Related Questions