@@ -23,7 +23,7 @@ use exec::{Exec, ExecNoSyncStr};
23
23
use expand:: expand_str;
24
24
use re_builder:: unicode:: RegexBuilder ;
25
25
use re_plugin:: Plugin ;
26
- use re_trait:: { self , RegularExpression , Locations } ;
26
+ use re_trait:: { self , RegularExpression , Locations , SubCapturesPosIter } ;
27
27
28
28
/// Escapes all regular expression meta characters in `text`.
29
29
///
@@ -927,6 +927,18 @@ impl<'t> Captures<'t> {
927
927
self . named_groups . pos ( name) . and_then ( |i| self . get ( i) )
928
928
}
929
929
930
+ /// An iterator that yields all capturing matches in the order in which
931
+ /// they appear in the regex. If a particular capture group didn't
932
+ /// participate in the match, then `None` is yielded for that capture.
933
+ ///
934
+ /// The first match always corresponds to the overall match of the regex.
935
+ pub fn iter < ' c > ( & ' c self ) -> SubCaptureMatches < ' c , ' t > {
936
+ SubCaptureMatches {
937
+ caps : self ,
938
+ it : self . locs . iter ( ) ,
939
+ }
940
+ }
941
+
930
942
/// Expands all instances of `$name` in `text` to the corresponding capture
931
943
/// group `name`, and writes them to the `dst` buffer given.
932
944
///
@@ -1025,6 +1037,29 @@ impl<'t, 'i> Index<&'i str> for Captures<'t> {
1025
1037
}
1026
1038
}
1027
1039
1040
+ /// An iterator that yields all capturing matches in the order in which they
1041
+ /// appear in the regex.
1042
+ ///
1043
+ /// If a particular capture group didn't participate in the match, then `None`
1044
+ /// is yielded for that capture. The first match always corresponds to the
1045
+ /// overall match of the regex.
1046
+ ///
1047
+ /// The lifetime `'c` corresponds to the lifetime of the `Captures` value, and
1048
+ /// the lifetime `'t` corresponds to the originally matched text.
1049
+ pub struct SubCaptureMatches < ' c , ' t : ' c > {
1050
+ caps : & ' c Captures < ' t > ,
1051
+ it : SubCapturesPosIter < ' c > ,
1052
+ }
1053
+
1054
+ impl < ' c , ' t > Iterator for SubCaptureMatches < ' c , ' t > {
1055
+ type Item = Option < Match < ' t > > ;
1056
+
1057
+ fn next ( & mut self ) -> Option < Option < Match < ' t > > > {
1058
+ self . it . next ( )
1059
+ . map ( |cap| cap. map ( |( s, e) | Match :: new ( self . caps . text , s, e) ) )
1060
+ }
1061
+ }
1062
+
1028
1063
/// An iterator that yields all non-overlapping capture groups matching a
1029
1064
/// particular regular expression.
1030
1065
///
0 commit comments