【发布时间】:2017-06-22 01:23:27
【问题描述】:
我有一些这种格式的哈希:
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
我有这样的字符串:
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”与我的字符串匹配
我使用了这段代码,但总是返回“否”
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");
}
【问题讨论】:
-
看起来好像您想使用 hash 列值作为正则表达式模式来测试字符串,对吧?只需提取它并将
?替换为.。 -
可以,但是 hash.Replace("?",".") 不起作用!
-
当然,首先提取哈希。
var pattern = hash.Split(':')[2].Replace("?", ".")然后Regex.IsMatch(str, pattern)。 -
它的工作,但我需要返回标题列作为匹配的结果