Reputation: 410
I am trying to manipulate and clean up a string of database columns as follows.
Example Source string(s):
[foo],[bar],baz
[foo],bar,[baz]
[foo],[bar,[baz]
[foo],bar],[baz]
foo,bar,baz
(and so on)
Expected output:
[foo],[bar],[baz]
I have tried to run the following regex substitutions over the string:
string columnString = "[foo],[bar],baz";
if (!Regex.IsMatch(columnString, @"^\[.*"))
{
columnString = string.Concat("[", columnString);}
if (!Regex.IsMatch(columnString, @"^.*\]$"))
{
columnString = string.Concat(columnString,"]");
}
while (!Regex.IsMatch(columnString, @"^.*\],.*$"))
{
columnString = Regex.Replace(columnString, @",", @"],");}
while (!Regex.IsMatch(columnString, @"^.*,\[.*$"))
{
columnString = Regex.Replace(columnString, @"\],", @"],[");
}
While this fixes up the leading and trailing brackets, it (obviously) doesn't deal with the commas where there is already an existing match in the string.
Can anyone suggest a method that would clean this up (it doesn't have to be regex).
Cheers
Upvotes: 1
Views: 464
Reputation: 626802
I suggest a splitting and string rebuilding solution:
var result = string.Join(
",",
s.Split(',') // split with commas
.Select(x => !x.StartsWith("[") && !x.EndsWith("]") ? $"[{x}]" : x ) // add [ ] to items not starting and ending with [ ]
);
See C# demo:
var strs = new List<string> { "[foo],[bar],baz", "[foo],bar,[baz]", "foo,bar,baz" };
foreach (var s in strs)
{
var result = string.Join(",", s.Split(',').Select(x => !x.StartsWith("[") && !x.EndsWith("]") ? $"[{x}]" : x ));
Console.WriteLine(result);
}
Output:
[foo],[bar],[baz]
[foo],[bar],[baz]
[foo],[bar],[baz]
Updated
As there may be items with either [
at the start or a ]
at the end you may use
var result = string.Join(
",",
s.Split(',')
.Select(x => !x.StartsWith("[") || !x.EndsWith("]") ?
$"[{Regex.Replace(x, @"^\[|]$", "")}]" : x
)
);
See this C# demo. Result:
[foo],[bar],[baz],[test]
[foo],[bar],[baz],[test]
[foo],[bar],[baz]
Note that Regex.Replace(x, @"^\[|]$", "")
removes a [
at the start and ]
at the end of the string.
Upvotes: 1
Reputation: 911
If you want to use regular expression, here is the answer:
var input = "[foo],bar,[baz]";
var regex = new Regex("((\\[?)((foo)|(bar)|(baz))(\\]?))");
var result = regex.Replace(input, "[$3]");
Please see: https://dotnetfiddle.net/Afnn3m
Upvotes: 0
Reputation: 2072
string str = "[foo],[bar],baz";
str = "[" + str.Replace("[", "").Replace("]", "").Replace(",", "],[") + "]";
Use StringBuilder
if possible. I just gave you an idea using String
class.
Upvotes: 1