Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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的形式谓词#_C#_Regex_String - Fatal编程技术网

C# 来自文本字符串c的形式谓词#

C# 来自文本字符串c的形式谓词#,c#,regex,string,C#,Regex,String,例如,我有以下模式: -输出{d}字节 -以{d}字节为单位 -In{d}bytesOut{d}字节 -输出{d}个单词 -用{d}字 -在{d}字中{d}字其中{d}是数字 我不喜欢案例。我怎样才能根据这段文字形成如下内容: p => p.Out == 8 && p.In == 16 //In8bytesOut16bytes p => p.Out == 8*8 && p.In == 4*8 // In8Out4words p => p.

例如,我有以下模式:
-输出{d}字节
-以{d}字节为单位 -In{d}bytesOut{d}字节
-输出{d}个单词
-用{d}字
-在{d}字中{d}字其中{d}是数字

我不喜欢
案例
。我怎样才能根据这段文字形成如下内容:

p => p.Out == 8 && p.In == 16 //In8bytesOut16bytes  
p => p.Out == 8*8 && p.In == 4*8 // In8Out4words  
p => p.In == 8 // In8bytes  
正则表达式?任何建议都会有帮助。提前谢谢

Match match;  
int inBytes = 0;  
int outBytes = 0;    
string pattern =   
@"(?<InOut>In|Out)(?<inBytes>\d+)bytes((?<Out>Out)(?     <outBytes>\d+)bytes)?";  
Func<string, IService, bool> predicate;  
match = Regex.Match(description, pattern);  
if ((match.Groups[4].Length > 0))  
{  
   int.TryParse(match.Groups[3].Value, out inBytes);  
   int.TryParse(match.Groups[5].Value, out outBytes);  
   predicate = p => p.In == inBytes && p.Out == outBytes;  
}  
匹配;
整数单位=0;
int-outBytes=0;
字符串模式=
@“(?输入|输出)(?\d+)字节((?输出)(?\d+)字节)?”;
Func谓词;
match=Regex.match(描述、模式);
if((match.Groups[4]。长度>0))
{  
int.TryParse(match.Groups[3].值,以字节为单位);
int.TryParse(match.Groups[5].值,out outBytes);
谓词=p=>p.In==inBytes&&p.Out==outBytes;
}  
输入为格式化字符串,该字符串取自文件。它应该满足上述模式之一。主要思想是从这个字符串中获取数字。有一个服务包含两个以字节为单位的定义,即以字节为单位的inOut。我需要解析此字符串并生成一个条件。
例如,我得到4bytesout8bytes。我需要得到4和8,并设定一个条件
这看起来像
Func predicate=p=>p.In==4和&p.Out==8

我建议您在这里使用而不是lambdas:

public class ServiceSpecification
{
    private const int BytesInWord = 8;
    public int? In { get; private set; }
    public int? Out { get; private set; }

    public static ServiceSpecification Parse(string s)
    {
        return new ServiceSpecification {
            In = ParseBytesCount(s, "In"),
            Out = ParseBytesCount(s, "Out")
        };
    }

    private static int? ParseBytesCount(string s, string direction)
    {
        var pattern = direction + @"(\d+)(bytes|words)";
        var match = Regex.Match(s, pattern);
        if (!match.Success)
            return null;

        int value = Int32.Parse(match.Groups[1].Value);
        return match.Groups[2].Value == "bytes" ? value : value * BytesInWord;
    }

    public bool IsSatisfiedBy(IService service)
    {
        if (In.HasValue && service.In != In.Value)
            return false;

        if (Out.HasValue && service.Out != Out.Value)
            return false;

        return true;
    }
}
您可以从输入字符串创建规范:

var spec = ServiceSpecification.Parse(text);
以及检查是否有任何服务符合本规范:

var specs = new List<ServiceSpecification>
{
    ServiceSpecification.Parse("In8bytesOut16bytes"),
    ServiceSpecification.Parse("In8wordsOut4words"),
    ServiceSpecification.Parse("In8bytes")
};

foreach(var spec in specs)
    Console.WriteLine(spec.IsSatisfiedBy(service));
var specs=新列表
{
解析(“in8bytesout16字节”),
解析(“In8wordsOut4words”),
ServiceSpecification.Parse(“In8bytes”)
};
foreach(规范中的变量规范)
控制台写入线(规范由(服务)满足);

A
开关
将比动态生成lambda表达式更快。此外,你在问题的第一部分中的模式与第二部分不同,而且还不完全清楚
Out
in
p
指的是什么。请包括一些代码,显示您迄今为止所做的尝试。“我不是一个有趣的案例”是什么意思?你喜欢工作的代码,还是工作的代码,不需要你成为一个有趣的案例?对不起,这是当然的。我认为对于
案例
太多的简单模式,这可以更容易地完成。输入是字符串。@Simon Belanger只为3种情况添加了代码,以{d}字节为单位,以{d}字节为单位,以{d}字节为单位,以{d}字节为单位,以{d}字节为单位,以{d}字节为单位,以{d}字节为单位,以{d}字节为单位,这种模式可以工作。@Mike我希望代码可以工作,但如果可以简化,为什么不可以。我同意,我的意思是,有办法让它变得更好。