C# 如何将正则表达式与十六进制值匹配?

C# 如何将正则表达式与十六进制值匹配?,c#,regex,hex,C#,Regex,Hex,我有这样的文本文件: 1:upx1:4D 00 68 6B 6A 68 6A:True 2:upx2:68 6B ?? 68 6A 00 02:False 3:upx3:FF 4D ?? 68 6B ?? 68:True 4D 5A 02 68 6B 6A 我有这样的字符串: 1:upx1:4D 00 68 6B 6A 68 6A:True 2:upx2:68 6B ?? 68 6A 00 02:False 3:upx3:FF 4D ?? 68 6B ?? 68:True 4D 5A 02

我有这样的文本文件:

1:upx1:4D 00 68 6B 6A 68 6A:True
2:upx2:68 6B ?? 68 6A 00 02:False
3:upx3:FF 4D ?? 68 6B ?? 68:True
4D 5A 02 68 6B 6A
我有这样的字符串:

1:upx1:4D 00 68 6B 6A 68 6A:True
2:upx2:68 6B ?? 68 6A 00 02:False
3:upx3:FF 4D ?? 68 6B ?? 68:True
4D 5A 02 68 6B 6A
谁与第3行匹配

我使用此代码检查正则表达式路径:

string readdb(string hash)
{
    using (StreamReader sr = new StreamReader("db.txt"))
    {
        string re = string.Format(@"(?<row>\w*:)(?<title>\w*:)({0}:)(?<ep>\w*)", hash);

        String line;
        while ((line = sr.ReadLine()) != null)
        {
            Regex regex = new Regex(re);
            Match match = regex.Match(line);
            if (match.Success)
            {
                return match.Groups[3].Value.ToString();
            }
        }
        return "0";

    }

}
string readdb(字符串哈希)
{
使用(StreamReader sr=newstreamreader(“db.txt”))
{
string re=string.Format(@“(?\w*:)(?\w*:)({0}:)(?\w*)”,哈希);
弦线;
而((line=sr.ReadLine())!=null)
{
正则表达式正则表达式=新正则表达式(re);
匹配=正则表达式匹配(行);
如果(匹配成功)
{
返回match.Groups[3].Value.ToString();
}
}
返回“0”;
}
}
但是我的问题是??登录文件

如何匹配任何十六进制值而不是双问号?

首先,移动
Regex Regex=new Regex(re)
while
循环之前,您将避免与regex对象娱乐相关的性能问题

接下来,您似乎需要2个十六进制字符的精确序列或一个双问号。您可以准备
散列
变量,以便以上述方式进行匹配:

hash = string.Join(" ", hash.Split().Select(x=>string.Format(@"(?:{0}|\?\?)", x)).ToArray());
string re = string.Format(@"(?<row>\w*):(?<title>\w*):([^:]*{0}[^:]*):(?<ep>\w*)", hash);
每个零件匹配2个十六进制字符或2个
s


请看一下。

如何使用
3:upx3:FF 4D??686B??68:True
match
4D 5A 02 68 6B 6A
?如果
hash
4D 5A 02 68 6B 6A
,则仅将“FF 4D?68 6B?68”与“4D 5A 02 68 6B 6A”匹配,将
re
字符串连接(“,hash.Split()。选择(x=>string.Format(@“(?:{0}\\?),”x))
,请参见.BTW,
Regex=new Regex)while
循环之前声明code>。我在第一行收到此错误:
与“string.Join(string,string[])”匹配的最佳重载方法有一些无效参数
参数2:无法从“System.Collections.Generic.IEnumerable”转换为“string[]”您是否使用System.Linq添加了
?无论如何,您可以在
Select
子句的末尾追加
.ToArray()
,将
IEnumerable
转换为
string[]
。i append.ToArray()并解决错误,但函数对于所有模式都返回0!那么,什么是字符串,什么是模式?您确定您的模式与输入字符串匹配吗?也许它不应该匹配。当我在文件中只有散列时,这个工作很好,例如:4D 00 68 02 6B 6A 68 6A 68 6B??02 68 6A 00 02 FF 4D??02686B??68但我有另一个值由“:”符号分隔