Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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_Stringbuilder - Fatal编程技术网

C# 使用正则表达式识别字符序列并用其生成字符串

C# 使用正则表达式识别字符序列并用其生成字符串,c#,regex,stringbuilder,C#,Regex,Stringbuilder,我有一个标记器,其中标记具有给定的接口: interface IToken { string Str { get; } //used after the token is built to get its content bool Success { get; } //has the informaition if the last 'TryAdd' was a success bool Valid { get; } //has the information if th

我有一个标记器,其中标记具有给定的接口:

interface IToken
{
    string Str { get; } //used after the token is built to get its content
    bool Success { get; } //has the informaition if the last 'TryAdd' was a success
    bool Valid { get; } //has the information if the current 'Str' is complete and Valid
    void TryAdd(char c); //Is called to add the next character to the token      
}
一般的想法是,使用给定输入的每个字符并行处理实现的令牌列表

“NumberToken”太慢了,我试着加快速度。我想用regex进行实验,我想知道下面的方法是否可行

给定一个类似于
stringpatternintfracexp=@“(\a)[0-9]+?\.[0-9]+?[eE]{1}[+-]{0,1}[0-9]+?(\Z)”我可以通过一次添加一个字符来增量检查表达式是否仍然有效。我已经编写了一些伪代码来说明我希望构建器做什么

class PseudoCode : IToken
{
    String patternINTFRACEXP = @"(\A)[0-9]+?\.[0-9]+?[eE]{1}[+-]{0,1}[0-9]+?(\Z)";

    RegexBuilder builder;

    public PseudoCode()
    {
        builder = new RegexBuilder(patternINTFRACEXP);
        Success = true;
    }

    public string Str{ get { return builder.ToString(); } }

    public bool Success { get; private set; }

    public bool Valid { get { return builder.IsMatch(); } }

    public void TryAdd(char c) { Success &= builder.TryAdd(c); }
}
StringBuilder和正则表达式的组合是否已经存在


实现
(builder作为RegexBuilder).TryAdd(c作为Char)的方法是什么

我的方法是对模式的不同步骤进行排序,从这样最长的步骤开始

([0-9]+\.[0-9]+[eE]{1}[+-]{0,1}[0-9]+|[0-9]+?\.[0-9]+[eE]{1}[+-]{0,1}[0-9]+|[0-9]+\.[0-9]+[eE]{1}[+-]{0,1}|[0-9]+\.[0-9]+[eE]{1}|[0-9]+\.[0-9]+|[0-9]+\.|[0-9]+)

我不熟悉c#,jsut在正则表达式部分的回答,不需要ungreedy操作符,因为您已经有了分隔符(,+,-,e),它们已经分割了字符串

我将使用以下不同部分制作一个数组:

pattern = ["[0-9]+","\.","[0-9]+","[eE]{1}","[+-]{0,1}","[0-9]+"]
testpattern = ""
pattern.each do |p|
  testpattern += "#{testpatern}#{p}|#{testpattern}" 
end
testpattern.rstrip("|") 
并使用testpaten进行测试。(在端部引出|)

它的ruby代码没有经过测试,所以我可能在某个地方有一个bug,但是我的想法是在每次迭代中添加新的部分,并最终得到完整的ORed正则表达式来进行测试

希望这会有所帮助

替代解决方案:
^([0-9]+)[.]([0-9]+)(?:[eE]{1}([+-]{0,1}[0-9]+)|)$

它精确匹配(如果格式中有不匹配的内容),并捕获整数部分、小数部分和带符号的指数(如果有)

它将匹配以e或e结尾的字符串,因此可能是您的验证部分

详细说明:


^
你是说。。。给定一个正则表达式和一个目标字符串,检查该字符串是否可能是最终与正则表达式匹配的字符串的开头?@Rawling Yes。这正是我的意思。我假设这在某种程度上是正则表达式实现的一部分。我只是不知道如何访问/使用这个请求。好吧,这是一个有趣的问题,我希望我知道答案:D我想如果你能进入正则表达式引擎,你会问“这是不是因为它到达了输入的末尾就失败了”,但你在C#中没有这种访问权限。您可以修改正则表达式以允许在每个字符后添加
$
,但这需要您自己编写正则表达式解析器。我希望其他人能想出一个有趣的解决方案。也许我需要进行更多的重构。web上有一篇有趣的文章说明了如何使用regex进行标记化:有趣,我可能会研究一下。然而,我觉得正则表达式的实现必须在一个字符流或类似的东西上运行——我正在寻找一个解决方案。我忘了提到,在这个解决方案中,您必须比较匹配长度和测试长度,即234z将在234中匹配,但捕获不会是完整的字符串。在我看来,正则表达式应该在字符串上工作,而不是在流上。@Johannes要进一步了解另一个问题,我想我会用它作为踏脚石,进入一个完全重构标记器的状态。