我正在尝试创建一个程序,用户可以输入一系列序列号并显示每个相应的产品。
假设我知道产品A始终以“C02”开头,产品B始终以“X02”结束,而产品C始终包含“A1700”。然后,如果用户输入为“C02HGV32,N93XA1700D,J3429X02”,则返回“C02HGV32:产品A; N93XA1700D:产品C; J3429X02:产品B”。
我如何获得一个Regex表达式数组来与字符串数组进行比较?这就是我所拥有的:
using System.Text.RegularExpressions;
public class ReturnProduct{
public Regex[] compareAgainst = new Regex[3]{@"[C02]*",@"*[X02]",@"*[A1700]*"}; //Clearly not the right way, but not sure how else to do it
...
public string getTheProduct(string input){
string[] compareString = input.Split(",");
for (int a = 0; a < compareString.Length; a++){
for (int b = 0; b < compareAgainst.Length; b++){
//Do something Regex-y with compareString[a] and compareAgainst[b]
}
}
答案 0 :(得分:2)
如果这些代码的要求非常简单,您可以使用String.Contains
,String.StartsWith
和String.EndsWith
。您可以创建Dictionary
来保存产品名称和函数,以检查给定字符串是否具有产品的模式。
var dict = new Dictionary<string, Predicate<string>>
{
["Product A"] = s => s.StartsWith("C02"),
["Product B"] = s => s.EndsWith("X02"),
["Product C"] = s => s.Contains("A1700")
};
string GetProductName(string serialNum)
{
foreach(var keyVal in dict)
{
if(keyVal.Value(serialNum))
return keyVal.Key;
}
return "No product name found";
}
List<(string, string)> GetProductNames(string str)
{
var productCodes = str.Split(',');
var productNames = new List<(string, string)>(); // list of tuples (string, string)
foreach(var serialNum in productCodes)
{
productNames.Add((serialNum, GetProductName(serialNum)));
}
return productNames;
}
用法:
var userString = "C02HGV32,N93XA1700D,J3429X02";
List<(string serialNum, string name)> productNames = GetProductNames(userString);
foreach(var tuple in productNames)
{
Console.WriteLine($"{tuple.serialNum} : {tuple.name}");
}
如果您特别想使用Regex,可以使用以下模式:
var regexDict = new Dictionary<string, Regex>
{
["Product A"] = new Regex("^C02"), //'^' means beginning of string
["Product B"] = new Regex("X02$"), //'$' means end of string
["Product C"] = new Regex("A1700") //given string anywhere
};
string GetProductName(string serialNum)
{
foreach(var keyVal in regexDict)
{
if(keyVal.Value.IsMatch(serialNum))
return keyVal.Key;
}
return "No product name found";
}
List<(string, string)> GetProductNames(string str)
{
var productCodes = str.Split(',');
var productNames = new List<string>();
foreach(var serialNum in productCodes)
{
productNames.Add((serialNum, GetProductName(serialNum)));
}
return productNames;
}
答案 1 :(得分:1)
为您的产品定义一个类:
public class Product
{
public string Name { get; set; }
public Regex Expr { get; set; }
}
然后创建一个包含所有正则表达式的数组:
var regexes = new[]
{
new Product
{
Name = "Product A",
Expr = new Regex("^C02")
},
new Product
{
Name = "Product B",
Expr = new Regex("X02$")
},
new Product
{
Name = "Product C",
Expr = new Regex("A1700")
}
};
现在您可以使用LINQ
查询:
var input = "C02HGV32,N93XA1700D,J3429X02";
var result = string.Join("; ",
input.Split(',')
.Select(s => new {regexes.FirstOrDefault(p => p.Expr.IsMatch(s))?.Name, Value = s})
.Select(x => $"{x.Value}: {x.Name}"));
result
将是
C02HGV32:产品A; N93XA1700D:产品C; J3429X02:产品B
答案 2 :(得分:0)
正则表达式语法:
“^。 A1700。 *” - 以任意数量的字符开头和结尾,并在某处包含A1700。
public static void GetTheProduct(string input, List<Regex> regList)
{
List<string> compareString = input.Split(new char[] { ',' }).ToList();
foreach (string item in compareString)
{
if (regList[0].Match(item).Success)
Console.WriteLine("{0} : {1}", item, "Product A");
else if (regList[1].Match(item).Success)
Console.WriteLine("{0} : {1}", item, "Product B");
else if (regList[2].Match(item).Success)
Console.WriteLine("{0} : {1}", item, "Product C");
}
}
static void Main(string[] args)
{
List<Regex> regexList = new List<Regex>() { new Regex("^C02.*"), new Regex("^.*X02"), new Regex("^.*A1700.*") };
GetTheProduct("C02HGV32,N93XA1700D,J3429X02", regexList);
Console.ReadLine();
}
您还可以概括该方法并避免硬编码产品名称。 像这样:
public static void GetTheProduct(string input, Dictionary<string, Regex> regDictionary)
{
List<string> compareString = input.Split(new char[] { ',' }).ToList();
foreach (string item in compareString)
{
string key = regDictionary.First(x => x.Value.IsMatch(item)).Key;
Console.WriteLine("{0} : {1}", item, key);
}
}
static void Main(string[] args)
{
Dictionary<string, Regex> regDictionary = new Dictionary<string, Regex>();
regDictionary.Add("Product A", new Regex("^C02.*"));
regDictionary.Add("Product B", new Regex("^.*X02"));
regDictionary.Add("Product C", new Regex("^.*A1700.*"));
GetTheProduct("C02HGV32,N93XA1700D,J3429X02", regDictionary);
Console.ReadLine();
}