Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on Andrews answer i have extended this a bit by creating a method to handle the replacement. In my situation i wanted to replace a whole group so i created a helper class to do this. Also it does not require you to create a pre and post capture group to achieve this.</p> <pre><code>/// &lt;summary&gt; /// A Regular Expression match and replace class /// &lt;/summary&gt; public class RegexReplacer { private readonly int _captureGroupToReplace; /// &lt;summary&gt; /// Initialises the RegEx replacer with matching criteria. /// &lt;/summary&gt; /// &lt;param name="name"&gt;A name that identifies this replacement pattern&lt;/param&gt; /// &lt;param name="matchCriteria"&gt;A regular Expression used to locate the values to replace&lt;/param&gt; /// &lt;param name="replacementValue"&gt;The value that will replace the matched pattern&lt;/param&gt; /// &lt;param name="captureGroupToReplace"&gt;The Capture group that should be replaced. The default is the entire match&lt;/param&gt; public RegexReplacer(string name, Regex matchCriteria, string replacementValue, int captureGroupToReplace = 0) { _captureGroupToReplace = captureGroupToReplace; Name = name; ReplacementValue = replacementValue; MatchCriteria = matchCriteria; } public string Name { get; set; } public Regex MatchCriteria { get; set; } public string ReplacementValue { get; set; } /// &lt;summary&gt; /// Finds and replaces all instances of a string within the supplied string or replaces a group if the group id is supplied in the constructor /// &lt;/summary&gt; public string ReplaceInString(string stringToSearch) { if (_captureGroupToReplace != 0) return MatchCriteria.Replace(stringToSearch, new MatchEvaluator(ReplaceGroup)); return MatchCriteria.Replace(stringToSearch, ReplacementValue); } private string ReplaceGroup(Match match) { try { var matchedString = match.Value; //Match index is based on the original string not the matched string int groupIndex = match.Groups[_captureGroupToReplace].Index - match.Index; int groupLength = match.Groups[_captureGroupToReplace].Length; var preGroupString = matchedString.Substring(0, groupIndex); var postGroupString = matchedString.Substring(groupIndex + groupLength, matchedString.Length - (groupIndex + groupLength)); var replacedString = String.Format("{0}{1}{2}", preGroupString, ReplacementValue, postGroupString); return replacedString; } catch (Exception) { return match.Value; } } } </code></pre> <p>I also had to modify my original pattern so it would give me an empty group at the end of the xml to insert an attribute if it didn't exist the usage looks like</p> <pre><code>var replacer = new RegexReplacer("DisplayCustomerAttribute", new Regex(@"(?:&lt;customer\s.*?((\sdisplay=""(?:yes|no)"").*?|())&gt;)"), @" display=""yes""", 1) xmlString = replacer.ReplaceInString(xmlString); </code></pre> <p>As a side note the reason for this is because the .net xml serialization does not include an attribute when the value is the same as the default. This is ok when your in control of the consumer but in our case we are not so we need to be explicit.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload