Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用变量作为正则表达式的输入来搜索字符串_C#_Regex - Fatal编程技术网

C# 如何使用变量作为正则表达式的输入来搜索字符串

C# 如何使用变量作为正则表达式的输入来搜索字符串,c#,regex,C#,Regex,我有一个格式为\d\w\^\d\d\d\d^\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 我的代码在字符串

我有一个格式为\d\w\^\d\d\d\d^\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;

}
}
}

找不到该字符串的原因是其中包含元字符。您可以通过将字符串传递给第一个:


codeType
的值是多少?它可能还需要
Regex.Escape
,因为
roomNumber
@Stribizev为上面的问题增加了字符串的值。@Phil由于字符串没有双引号围绕您正在搜索的字符串,您还需要删除Regex中的引号。@dasblinkenlight我知道了。因为它是一个对象,所以我需要获取该对象的单个路径字符串(因为它是一个自定义类,所以除了我之外,任何人都不会知道这一点)。不过,一旦我从对象中拉出正确的字符串,Regex.Escape就起作用了。谢谢它仍然没有对正则表达式产生影响。将字符串的值添加到上述问题中这是由于我的ToString方法是如何创建字符串的。有一次我把你关于正则表达式的建议拉到了一条单独的路径上,Escape成功了。谢谢
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);


});
Regex roomSearch = new Regex(Regex.Escape(roomNumber), RegexOptions.IgnoreCase | RegexOptions.Compiled);