有一种方法可以反转RegEx.MatchCollection中的项吗?
例如,如果MatchCollection包含以下匹配项:
a1
a2
a3
然后我想要反转集合项,这样项索引就是其他值:
a3
a2
a1
保留它们的匹配索引和其他匹配信息,如长度、组等。
与MatchCollection
有些不同,但是它是一个IEnumerable<Match>
myMatchCollection.Cast<Match>().Reverse()
VB.NET 版本:
Imports System.Text.RegularExpressions
Dim matches() As Match = Regex.Matches("ABCDA", "A").Cast(Of Match).Reverse().ToArray()
MsgBox(matches.First.Index) ' Result: 4
MatchCollection
还是可以直接获取Matches
并将其反转,例如:var reversed = matches.Cast<Match>().Reverse();
- sa_ddam213