It replaces leading and trailing spaces inside a specific attribute’s value, globally (as per this example page: http://lloydi.com/x/re/).
I need to do the same on a string in C#, but when trying the above, the syntax checker chokes at the regExp. So I need the equivalent line of the above in C#.
I’m not really a C# person, to be honest. Out of my comfort zone!
Thanks for the replies. I’m afraid I’m a doofus where C# is concerned. Truth be known, I’m asking on someone else’s behalf (my significant other) who’s getting frustrated that she can’t get it running, and I’m getting frustrated that I’m not able to help
Anyway … this is kind of where we are.
//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);
So it won’t build because of the syntax problems. I guess I’m after some specific syntax help based on the above, someone to point out what needs changing to make this work. I’ve got a working example in JavaScript, which is here:
… so it can’t be that far off. It shows two alerts - one before and one after the replace method.
If someone’s able to point out the corrections to the above, it would be really appreciated. If it’s easier to run the example yourself to do a sanity check, here’s a string that you can copy (It’s all on one line so should be easy to triple-click, copy):
<xml><elementName specificattribute=" 111 222 333333 " anotherattribute="something" somethingelse="winkle"><someotherelement>value of some kind</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName><elementName specificattribute=" 444 444 7777 " anotherattribute="monkey" somethingelse="blahdeblah"><someotherelement>purple</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName><elementName specificattribute=" 1 2 3 4 5 6" anotherattribute="chinchilla" somethingelse=""><someotherelement>green</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName><elementName specificattribute="1 1 111 33 " anotherattribute="owl" somethingelse=""><someotherelement>tellow blue brown</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName></xml>
Once again, would really appreciate help on this. It must almost be there … tantalizingly close!
@paro. That is actually a very good solution. Much better that regex in my honest opinion. I would def go with this solution as apposed to the regex. If you can use Linq. .NET 3.0 and up.