I have a string that I need to do multiple search and replaces to remove leading and trailing spaces inside an attribute. The before and after effect is shown here (visually and with a JS example of it working):
Now, I need to do the equivalent in C# - replace all references in a string. But I am really stuck. I know the pattern is correct, as shown in the JS version, but the syntax/escape syntax is doing my head in.
Here's what I have, but of course it doesn't work ;-)
//define the string
string xmlString = "<xml><elementName specificattribute=" 111 222 333333 " anotherattribute="something" somethingelse="winkle"><someotherelement>value of some kind</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName></xml>";
// here's the regExPattern - the syntax checker doesn't like this at all
string regExPattern = "/(specificattribute=)"\s*([^"]+?)\s*"/g";
// here's the replacement
string replacement = "$1\"$2\"";
Regex rgx = new Regex(regExPattern);
string result = rgx.Replace(xmlString, replacement);
Can someone tell me the error of my ways?
Many thanks!