Get combinations with spaces for initials (letter followed by dot)
For input text like "A.B.C.D. EFG", I would like to get permutations of letters followed by dots to be converted to add space after the dot. So for the example above, I would like to get "A.B.C.D.", "A. B.C.D.", "A.B. C.D.", "A.B开发者_运维技巧.C. D.", "A. B. C.D.", "A.B. C. D.", "A. B. C. D."
Can this be done with regex ? If yes then any regex sample that works with c# (.net) would be most appreciated. The number of characters with dots is not known and can vary from text to text.
On a side note, if the text is "A. B. C. D. EFG" or "A. B.C.D. EFG" etc, I would like to get all permutations for that.
Regards
Something like
static string[] GetPermutations(string input)
{
List<string> ret = new List<string>();
List<string> cleanInput = new List<string>();
foreach (string bit in input.Split('.'))
{
if (bit.Trim().Length > 0) cleanInput.Add(bit.Trim());
}
foreach (string bit in cleanInput)
{
if (ret.Count == 0)
{
ret.Add(bit);
continue;
}
List<string> oldRet = ret;
ret = new List<string>();
foreach (string oldBit in oldRet)
{
ret.Add(oldBit + bit);
ret.Add(oldBit + " " + bit);
}
}
return ret.ToArray();
}
Then, to call it:-
foreach (string p in GetPermutations("A.B. C.D."))
{
Console.WriteLine(p);
}
It outputs:-
ABCD
ABC D
AB CD
AB C D
A BCD
A BC D
A B CD
A B C D
You just need to add the dots in, and check any other unspecified logic you may need (eg, stripping away the EFG's from your example input.
精彩评论