Reputation: 10509
Given the following partial file example :
<SellGrey>True</SellGrey>
<SellWhite>false</SellWhite>
<SellGreen>false</SellGreen>
<SellBlue>false</SellBlue>
What is the best method to do a case-insensitive search and replace, while keeping the proper case on output.
For example:
<SellGrey>True</SellGrey>
or
<Sellgrey>tRue</Sellgrey>
would be what I am searching for, but the replacement, would always be :
<SellGrey>False</SellGrey>
Lastly, please do not get hung up on the "tags" as the file is malformed xml so reading/writing xml would bugger things up. Please look at the strings as just that -- a case-insensitive string search and replace, on a line by line basis.
Thanks in advance.
Upvotes: 1
Views: 855
Reputation: 14477
Since you didn't provide much detail about the tags, I used a dictionary for proper casing.
Dim input as String = "<Sellgrey>tRue</Sellgrey>"
Dim pattern as String = "<(?<tag>.+)>(?<value>(true|false))</.+>"
'building a dictionary to specify how to proper case
Dim tagFormatter as new Dictionary(Of String, String)
tagFormatter.Add("sellgrey", "ShellGrey")
tagFormatter.Add("sellwhite", "SellWhite")
tagFormatter.Add("sellgreen", "SellGreen")
tagFormatter.Add("sellblue", "SellBlue")
'build the new string using lambda
Dim result = Regex.Replace(input, pattern, _
Function(m) String.Format("<{0}>{1}</{0}>", _
tagFormatter(m.Groups("tag").Value.ToLower), _
If(m.Groups("value").Value = "true", "True", "False")), _
RegexOptions.IgnoreCase)
Upvotes: 1