Zesty
Zesty

Reputation: 3001

How can I replace this pattern with Regex?

I need to replace all / with \ in all matches of the pattern src="" within a string.

I.e., the pattern starts with src=" and ends with "

There could be multiple matches within the string.

I've tried this but it didn't match:

"src=\"^*\"$"

Upvotes: 0

Views: 93

Answers (2)

Jens
Jens

Reputation: 25593

You can use

String input = @"...src=""/foo/bar""..";
String output = Regex.Replace(input, "src=\"[^\"]*\"", (m) => m.ToString().Replace('/', '\\'));

Upvotes: 1

Simon
Simon

Reputation: 4585

Does your string always end with the closing " ? If not: remove the $ !

Upvotes: 1

Related Questions