我似乎无法使用此代码
private void test()
{
int success = 0;
string strLine = "E0 Q0 V1 X4 &C1 &D1 &R1 &S0 +IFC= 2,2 &K3 +FCLASS0";
if (Regex.IsMatch(strLine, "E6 Q1 V4 X8 &C2 &D1 &R1 &S0 +IFC= 4,4 &K4 +FCLASS2"))
success++;
if (Regex.IsMatch(strLine, "E0 Q0 V1 X4 &C1 &D1 &R1 &S0 +IFC= 2,2 &K3 +FCLASS0"))
success++;
LogMessage("Success =" + success.ToString()); // SENDS DATA TO LOG SCREEN
}
private void TESTbutton_Click(object sender, EventArgs e)
{
test();
}
17:39:29.98 - 成功= 0
我需要完全匹配它并在操作符“+& - ”之前用反斜杠“\”尝试它,这是什么使这成为真的正确方法?
答案 0 :(得分:1)
如果您坚持使用正则表达式,则必须转义关键符号(+
,*
等):
String toFind = "E0 Q0 V1 X4 &C1 &D1 &R1 &S0 +IFC= 2,2 &K3 +FCLASS0";
// String = Regex.Escape(toFind); // for "contains"
String pattern = "^" + Regex.Escape(toFind) + "$"; // ^ ... $ for exact match
if (Regex.IsMatch(strLine, pattern))
success++;
但是,您似乎只能比较字符串:
String toFind = "E0 Q0 V1 X4 &C1 &D1 &R1 &S0 +IFC= 2,2 &K3 +FCLASS0";
// Exact match
if (String.Equals(strLine, toFind, StringComparison.Ordinal))
success++;
// Contains
// if (strLine.Contains(toFind))
// success++;