我有一个像这样的字符串
"{{A to B}}{{1 to 3}}"
它应该生成:
A1
A2
A3
B1
B2
B3
请注意,模式字符串中可能有任意数量的模式。
解决这个问题的最佳方法是什么?解决方案首选.Net。
答案 0 :(得分:1)
如评论中所述,仅使用正则表达式无法解决此问题。以下是一些您可能会觉得有用的代码:
var input = "{{A to B}}{{1 to 3}}{{a to f}}";
var regex = new Regex("\\{\\{((?<p1>[a-z]) to (?<p2>[a-z]))|((?<p1>[A-Z]) to (?<p2>[A-Z]))|((?<p1>\\d) to (?<p2>\\d))\\}\\}");
var groups = new List<List<string>>();
foreach (Match match in regex.Matches(input))
{
var start = match.Groups["p1"].Value;
var end = match.Groups["p2"].Value;
var group = new List<string>();
int startValue;
int endValue;
if (int.TryParse(start, out startValue) && int.TryParse(end, out endValue))
{
for (int i = startValue; i <= endValue; i++)
{
group.Add(i.ToString());
}
}
else
{
var startChar = char.Parse(start);
var endChar = char.Parse(end);
for (char c = startChar; c <= endChar; c++)
{
group.Add(c.ToString());
}
}
groups.Add(group);
}
var cartesianProduct = groups.CartesianProduct();
var result = cartesianProduct.Select(x => string.Concat(x.ToArray()));
foreach (var r in result)
{
Console.WriteLine(r);
}
为了计算笛卡尔积,我使用了来自here的代码:
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
foreach (var sequence in sequences)
{
var s = sequence;
result =
from seq in result
from item in s
select seq.Concat(new[] { item });
}
return result;
}
我错过了有效输入的所有检查,你必须自己处理。
答案 1 :(得分:1)
for循环怎么样?使用正则表达式:
string input = "{{A to B}}{{1 to 3}}";
Match match = Regex.Match(input, @"\{\{([A-Za-z])\sto\s([A-Za-z])\}\}\{\{([\d])\sto\s([\d])\}\}");
List<char> rangeChar = new List<char>(){match.Groups[1].Value[0],match.Groups[2].Value[0]};
List<int> rangeInt = new List<int>() { int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value) };
for (int i =rangeChar.First() ; i<=rangeChar.Last();i++)
for (int j = rangeInt.First(); j <= rangeInt.Last(); j++)
{
Console.WriteLine((char)i+j.ToString());
}
没有:
string input = "{{A to B}}{{1 to 3}}";
string[] inputSplit = input.Split(new[] {@"}{"},StringSplitOptions.None);
List<char> rangeChar = new List<char>();
inputSplit[0].Trim(' ', '{', '}').Split(new[] { "to" }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(x => rangeChar.Add(x.Trim()[0]));
List<int> rangeInt = new List<int>();
inputSplit[1].Trim(' ', '{', '}').Split(new[] { "to" }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(x=> rangeInt.Add(int.Parse(x)));
for (int i =rangeChar.First() ; i<=rangeChar.Last();i++)
for (int j = rangeInt.First(); j <= rangeInt.Last(); j++)
{
Console.WriteLine((char)i+j.ToString());
}