如何将变量用作对正则态度的输入以搜索字符串



i具有 d w ^ d d d d d d d d^ w w w的格式的变量。我要做的是传递实际变量,在这种情况下,硬编码变量为1B^1001^01,将其放入正则态度,以便我可以通过字符串解析并在发现的情况下传递真实值。

objPtFqr =  SIPE-NAE-001:SIPE-NAE-001/Programming.CommonPath.1B^1001^01.Code_Blue,object,JCI BV
roomNumber = 1B^1001^01 
codeType = Code_Blue

我的代码没有在字符串中找到变量。这是使用正则条件的方法。

public JciFqr ParseHL7Fqr(List<JciFqr> objPtFqrs, string roomNumber, string codeType)
{
List<JciFqr> folderParsedFQRs = new List<JciFqr>();
JciFqr parsedObjPtFqr = null;
Regex roomSearch = new Regex(@"""" + roomNumber + @"""", RegexOptions.IgnoreCase | RegexOptions.Compiled);
foreach (JciFqr objPtFqr in objPtFqrs)
{
    var result1 = roomSearch.Match(objPtFqr.ToString());
    if (result1.Success)
    {
        folderParsedFQRs.Add(objPtFqr);
    }
}
foreach (JciFqr folderParsedFQR in folderParsedFQRs)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(folderParsedFQR.ToString(), codeType, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
    {
        parsedObjPtFqr = folderParsedFQR; ;
    }
}
return parsedObjPtFqr;
}
}
}

这是调用方法的代表:

var delegatedTask1 = Task.Run(async () =>
{
await objClient.OpenAsync(username, password, CancellationToken.None);
JciFqr objRootFqr = objClient.CreateFqr(siteDirector, path, JciFqr.Classifications.Folder, JciFqr.Types.Folder);
aobjRoomFqrs = await objClient.GetObjectsAsync(objRootFqr, CancellationToken.None);

foreach (JciFqr objFqr in aobjRoomFqrs)
{
    JciFqr objCodeBlueFqr = JciFqr.Create(objFqr, "Code_Blue", JciFqr.Classifications.Object, JciFqr.Types.BinaryValue);
    objPtFqrs.Add(objCodeBlueFqr);
}
parsedObjPtFqr = hl7Event.ParseHL7Fqr(objPtFqrs, roomNumber, codeType);
await objClient.WritePropertyAsync(parsedObjPtFqr, "Present Value", true ? "on" : "off", a_strPriority, CancellationToken.None);

});

未发现字符串的原因是它中有元关节。您可以首先将字符串传递到Escape

来解决该问题
Regex roomSearch = new Regex(Regex.Escape(roomNumber), RegexOptions.IgnoreCase | RegexOptions.Compiled);

最新更新