如何在字符串中使用通配符搜索十六进制模式? [英] How to search hex pattern with wild card in string?

查看:307
本文介绍了如何在字符串中使用通配符搜索十六进制模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这种格式的哈希值:

i have some hashes in this format :

row:title:hash:flag
1:upx1:4D 00 68 6B ?? 68 6A:True
2:upx2:68 6B ?? 68 6A 00 02:False
3:upx3:FF 4D ?? 68 6B ?? 68:True

我有这样的字符串:

and i have string like this:

02 4D 00 68 6B 6A 68 6A 00 02 00 00 00 FF 02 5A 68 6B 6A 68 6A 00 02 00

我需要将任何哈希与该字符串匹配,并且将任何十六进制值匹配,而不是双问号
例如第1行中的"4D 00 68 6B ?? 68 6A"中的哈希与我的字符串

i need to match any hash with this string and match any hex value instead of double question mark
for example hash in row 1 "4D 00 68 6B ?? 68 6A" match with my string

我使用了此代码,但始终返回"no"

i used this code but always return "no"

    string str = "02 4D 00 68 6B 6A 68 6A 00 02 00 00 00 FF 02 5A 68 6B 6A 68 6A 00 02 00";
    string hash = "1:upx1:4D 00 68 6B ?? 68 6A:True";

    str = string.Join(" ", str.Split().Select(x => string.Format(@"(?:{0}|\?\?)", x)).ToArray());
    string sPattern = string.Format(@"(?<row>\w*:)(?<title>\w*:)([^:]*{0}[^:]*:)(?<ep>\w*)", hash);


    if (Regex.IsMatch(str, sPattern))
    {
        MessageBox.Show("ok");
    }
    else
    {
        MessageBox.Show("no");
    }

推荐答案

您不希望整个行都与string匹配,因为string不适合该行.这是一个有效的示例:

You don't want the whole row to be matching to string because string doesn't fit in the row. here is a working example of how to do it:

 public static void Test()
 {
       string str = "02 4D 00 68 6B 6A 68 6A 00 02 00 00 00 FF 02 5A 68 6B 6A 68 6A 00 02 00";
        string hash = "1:upx1:4D 00 68 6B ?? 68 6A:True";
        var parts = hash.Split(':');
        string title = parts[1];
        string hashhex = parts[2];
        string sPattern = hashhex.Replace("?", ".");
        Console.WriteLine($"Pattern={sPattern}");
        Console.WriteLine($"String={str}");

        if (Regex.IsMatch(str, sPattern))
        {
            Console.WriteLine("ok");
            Console.WriteLine($"MatchedTitle={title}");
        }
        else
        {
            Console.WriteLine("no");
        }
        Console.ReadLine();
 }

这是输出:

这篇关于如何在字符串中使用通配符搜索十六进制模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆