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_.net 3.5_Sql Server 2016 - Fatal编程技术网

C# 为什么被动(非捕获)组在使用后向查看时比正常组表现更好?

C# 为什么被动(非捕获)组在使用后向查看时比正常组表现更好?,c#,regex,.net-3.5,sql-server-2016,C#,Regex,.net 3.5,Sql Server 2016,不起作用:(\d+\s*|)(?您的问题是,当正则表达式应用于12-34时,捕获组的值在第一个正则表达式中为4,在第二个正则表达式中为12-34 正则表达式的结构如下(公共部分突出显示) 不同之处在于,在第一次迭代中,您重复一个捕获组。捕获的值仅包含上一次迭代的结果 在第二个示例中,您捕获了一个重复的组。这是用于您所需语义的正确组 请参阅更多有关此内容。不存在谎言,我没有指责你,但不完整的信息。现在,当你提供了平台-MSSQL和NET3.5,并为我们运行测试时,我投票重新打开你的问题。“德米特

不起作用:
(\d+\s*|)(?您的问题是,当正则表达式应用于
12-34
时,捕获组的值在第一个正则表达式中为
4
,在第二个正则表达式中为
12-34

正则表达式的结构如下(公共部分突出显示)

不同之处在于,在第一次迭代中,您重复一个捕获组。捕获的值仅包含上一次迭代的结果

在第二个示例中,您捕获了一个重复的组。这是用于您所需语义的正确组


请参阅更多有关此内容。

不存在谎言,我没有指责你,但不完整的信息。现在,当你提供了平台-MSSQL和NET3.5,并为我们运行测试时,我投票重新打开你的问题。“德米特里,它仍然是DUPE,请考虑重合。如果你离开我,我可以修改自己的密切理由。评论。关于重复捕获grohps和它们的行为,有很多问题,不需要成倍增加。对这个正确答案的否决票的一些解释将是值得赞赏的。新来者不能投票,我只将你的答案标记为正确,但我不能更改评级…@SaSha-是的,这不是针对你的
"12-34" gives "12-34" (correct) versus "4"  (incorrect)
"1-23"  gives "1-23"  (correct) versus "3"  (incorrect)
"12-3"  gives "12-3"  (correct) versus "-3" (incorrect)
using System.Data.SqlTypes; //SqlInt32, ...
using Microsoft.SqlServer.Server; //SqlFunction, ...
using System.Collections; //IEnumerable
using System.Collections.Generic; //List
using System.Text.RegularExpressions;
internal struct MatchResult {
    /// <summary>Which match or group this is</summary>
    public int ID;
    /// <summary>Where the match or group starts in the input string</summary>
    public int Pos;
    /// <summary>What string matched the pattern</summary>
    public string Match;
}
public class RE {
    [SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true, SystemDataAccess = SystemDataAccessKind.None, FillRowMethodName = "FBsRow")]
    public static IEnumerable FBs(string str, string pattern, SqlInt32 opt) {
        if (str == null || pattern == null || opt.IsNull) return null;
        var gs = Regex.Match(str, pattern, (RegexOptions)opt.Value).Groups; int gid = 0; List<MatchResult> r = new List<MatchResult>(gs.Count);
        foreach (Group g in gs) r.Add(new MatchResult { ID = gid++, Pos = g.Index, Match = g.Value }); return r;
    }

    public static void FBsRow(object obj, ref SqlInt32 ID, ref SqlInt32 Pos, ref string FB) { MatchResult g = (MatchResult)obj; ID = g.ID; Pos = g.Pos; FB = g.Match; }
}
go
sp_configure 'clr enabled', 1
RECONFIGURE WITH OVERRIDE
go
IF OBJECT_ID(N'dbo.FBs') IS NOT NULL DROP FUNCTION dbo.FBs
go
go
if exists(select 1 from sys.assemblies as A where A.name='SQL_CLR') DROP     ASSEMBLY SQL_CLR
go
CREATE ASSEMBLY SQL_CLR FROM 'C:\src\SQL_CLR.dll'
go
CREATE FUNCTION dbo.FBs(@str nvarchar(max),@pattern nvarchar(max),@opt int=1)
RETURNS TABLE (ID int,Pos int,FB nvarchar(max)) WITH EXECUTE AS CALLER
AS EXTERNAL NAME SQL_CLR.[RE].FBs
go
;with P(p) as (select * from (values ('(\d+\s*|(?<=\d+\s*)-\s*\d\s*)+'),('((?:\d+\s*|(?<=\d+\s*)-\s*\d\s*)+)')) P(t)),
T(t) as (select * from (values ('12-34'),('12-3'),('1-23'),('1234'),('123')) T(t))
select *,iif(t=FB,'PASS','FAIL') from P cross join T outer apply dbo.FBs(t,p,0) where ID=1
go