Regexp help - converting from JS match to C#

This is probably a simple one for C# people :slight_smile:

I have this regular expression in javascript:

strVar = strVar.replace(/(specificattribute=)“\s*([^”]+?)\s*“/g, '$1”$2"');

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!

Can anyone help?

You first gona need this:
Regex reg = new Regex(“normal regex pattern here”);

but you do not need the trailing /g. That tells js that its global. That is not needed in C#

Then you can do something like this:
strVal = reg.Replace(stringContainingText, “”);

I hope this points you in the right direction

Because of the frequency of backslashes, you will probably want to use an unescaped string literal:

Regex r = new Regex(@“\d\s\w”);

Cheers,
D.

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 :frowning:

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:

http://lloydi.com/x/re/test.html

… 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!

I’d probably go a completely different route:


XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString);

Then loop through each element, and within each element, loop through the attribute collection and call Trim() on that value.

As for the regex:

"/(specificattribute=)"\\s*([^"]+?)\\s*"/g"
  • You don’t need the global flag.
  • You don’t use forward-slash delimiters.
  • I assume “(specificattribute=)” will be replaced with (\w+=)

Cheers,
D.

And as stated above, use a string literal:

string regExPattern = @“/(w+=)”“\s*([^”“]+?)\s*”“”;

That should do the trick. I am not sure if your last " is needed or not, so I just added it anyway.

As pointed out above, I forgot to mention that you should always use a literal string. It will make life a lot easier.

Just keep in mind what they say: You have a problem. You fix it with Regex. Now you have 2 problems. LOL

Here is a non-regex way you could do it.


void ClearSpaces(XElement element)
{
    element.Attributes().ToList().ForEach(a => a.Value = (a.Value != null ? a.Value.Trim() : string.Empty));
    element.Elements().ToList().ForEach(e => ClearSpaces(e));
}

and the calling code…


XElement e = XElement.Parse("your xml string here");

ClearSpaces(e);

// do something with e

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

Thanks for your help folks - have passed on :slight_smile: