RegEx Help - Converting from JavaScript to C #

I have a line in which I need to do a multiple search and replace to remove leading and trailing spaces inside an attribute. Here is the effect before and after (visually and with an example of its operation):

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

Now I need to make an equivalent in C # - replace all links in a string. But I'm really stuck. I know that the template is correct, as shown in the JS version, but the syntax of the / escape syntax makes my head.

Here is what I have, but of course this does not 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 the regExPattern - the syntax checker doesn't like this at all string regExPattern = "/(specificattribute=)"\s*([^"]+?)\s*"/g"; // here 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!

+4
source share
3 answers

Do not use regular expressions for this task. .NET has powerful tools for manipulating XML documents. Try instead:

 XDocument doc = XDocument.Load("input.xml"); foreach (XAttribute attr in doc.Descendants("elementName") .Attributes("specificattribute")) { attr.Value = attr.Value.Trim(); } doc.Save("output.xml"); 
+3
source

Remove / g at the end of regExPattern. This is the first mistake that I see for sure. The .NET regex implementation does not have a global modifier, by default it is global.

UPDATE:

I think this should 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 the regExPattern - the syntax checker doesn't like this at all string regExPattern = "(specificattribute=)\"\\s*([^\"]+?)\\s*"; // here the replacement string replacement = "$1\"$2\""; Regex rgx = new Regex(regExPattern); string result = rgx.Replace(xmlString, replacement); 

Although this may work for you, the XML-nested / context-sensitive nature makes regular expressions unsuitable for proper and efficient parsing. This, of course, is not the best tool for the job, so to speak.

From the look of things, you should really use something like Xpath or Linq to XML to parse and modify these attributes.

I practically steal Mark Bayer's answer, but since his example is with xml files, and you do it in memory, it should be something like this:

 XDocument doc = XDocument.Parse("<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>"); foreach (XAttribute attr in doc.Descendants("elementName") .Attributes("specificattribute")) { attr.Value = attr.Value.Trim(); } string result = doc.ToString(); 
+2
source

Seriously, you should use the System.Xml class for this. Here is another example: XPath :

  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>"; XmlDocument xml = new XmlDocument(); ; xml.LoadXml(xmlString); foreach (XmlAttribute el in xml.SelectNodes("//@specificattribute")) { el.Value = el.Value.Trim(); } 
0
source

Source: https://habr.com/ru/post/1300352/


All Articles