You could use Eric Lippert's code to produce combinations to implement this.
Here's a demonstration. The method you want to call is Combinations()
- it accepts a pattern as per your requirements and outputs a sequence of combinations:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string pattern = "AA00";
foreach (var s in Combinations(pattern))
Console.WriteLine(s);
}
public static IEnumerable<string> Combinations(string pattern)
{
string letters = "abcdefghijklmnopqrstuvwxyz";
string digits = "0123456789";
var sets = pattern.Select(ch => ch == 'A' ? letters : digits);
return Combine(sets).Select(x => new String(x.ToArray()));
}
public static IEnumerable<IEnumerable<T>> Combine<T>(IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] { item }));
}
}
}
Note: I have omitted all argument checking and validation for the sake of brevity.
[EDIT] Here's the example extended to show how you can add other char sets:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
internal class Program
{
private static void Main(string[] args)
{
string pattern = "A0*ë";
foreach (var s in Combinations(pattern))
Console.WriteLine(s);
}
public static IEnumerable<string> Combinations(string pattern)
{
var sets = pattern.Select(charset);
return Combine(sets).Select(x => new String(x.ToArray()));
}
private static string charset(char charsetCode)
{
switch (charsetCode)
{
case 'A': return "abcdefghijklmnopqrstuvwxyz";
case '0': return "0123456789";
case '*': return "!£$%^&*()_+=-";
case 'ë': return "àáâãäåæçèéêë";
// Add new charset codes and charsets here as desired.
default: throw new InvalidOperationException("Bad charset code: " + charsetCode);
}
}
public static IEnumerable<IEnumerable<T>> Combine<T>(IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] {Enumerable.Empty<T>()};
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] {item}));
}
}
}