实际上,在这种情况下,我必须验证是否存在重叠模式(可能是正则表达式或其他什么)。
string pattern = "123*"
string[] patterns = new string[] { "123?", "123*", "12?", "*23*" }我相信这里重叠的匹配项是"123?"、"123*"和"*23*"
任何尾随字符的通配符限制为"*",长度为1的通配符限制为"?"
顺便说一句,这是针对数字格式的,因此也有可能在数字模式/格式中包含文字破折号(最多)。
这可以用正则表达式或其他方法来完成吗?如果可能的话,使用C# / Javascript。
请对我错过的任何信息发表评论。
提亚
发布于 2018-05-24 08:39:40
我只想分享我目前的解决方案,但这是通过C#实现的。
static string regRep(string inp)
{
return inp.Replace("*", ".*").Replace('?', '.').Replace("-", @"\-");
}
static string regRep2(string inp)
{
return inp.Replace("*", ".*").Replace('?', '.').Replace("-", ".");
}
static void Main()
{
var patterns = new string[] { "8888*", "8889*", "8898*", "8899*", "8988*", "8989*", "8989*", "8999*", "7898*", "7899*", "8888?8", "888?-8", "*88*" };
string ar = @"888?-*";
List<string> res = new List<string>();
bool mat1, mat2;
Regex reg1, reg2;
foreach (var reg in patterns)
{
reg1 = new Regex(regRep(reg));
reg2 = new Regex(regRep(ar));
mat1 = reg1.IsMatch(ar);
mat2 = reg2.IsMatch(reg);
// standard matching
if (mat1 || mat2)
{
res.Add($"{ar} std matched with {reg}");
}
else
{
//forced matching
reg1 = new Regex(regRep2(reg));
reg2 = new Regex(regRep2(ar));
mat1 = reg1.IsMatch(ar);
mat2 = reg2.IsMatch(reg);
if (mat1 || mat2)
{
res.Add($"{ar} frc matched with {reg}");
}
}
}
}
//result
Count = 5
[0]: "888?-* frc matched with 8888*"
[1]: "888?-* frc matched with 8889*"
[2]: "888?-* frc matched with 8888?8"
[3]: "888?-* std matched with 888?-8"
[4]: "888?-* std matched with *88*"这一个目前对我有效,我希望有人也会,如果不是,更好。谢谢!
https://stackoverflow.com/questions/50478228
复制相似问题