File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -789,6 +789,22 @@ impl<'t> Captures<'t> {
789
789
/// Returns the match associated with the capture group at index `i`. If
790
790
/// `i` does not correspond to a capture group, or if the capture group
791
791
/// did not participate in the match, then `None` is returned.
792
+ ///
793
+ /// # Examples
794
+ ///
795
+ /// Get the text of the match with a default of an empty string if this
796
+ /// group didn't participate in the match:
797
+ ///
798
+ /// ```rust
799
+ /// # use regex::bytes::Regex;
800
+ /// let re = Regex::new(r"[a-z]+(?:([0-9]+)|([A-Z]+))").unwrap();
801
+ /// let caps = re.captures(b"abc123").unwrap();
802
+ ///
803
+ /// let text1 = caps.get(1).map_or(&b""[..], |m| m.as_bytes());
804
+ /// let text2 = caps.get(2).map_or(&b""[..], |m| m.as_bytes());
805
+ /// assert_eq!(text1, &b"123"[..]);
806
+ /// assert_eq!(text2, &b""[..]);
807
+ /// ```
792
808
pub fn get ( & self , i : usize ) -> Option < Match < ' t > > {
793
809
self . locs . pos ( i) . map ( |( s, e) | Match :: new ( self . text , s, e) )
794
810
}
Original file line number Diff line number Diff line change @@ -934,6 +934,22 @@ impl<'t> Captures<'t> {
934
934
/// Returns the match associated with the capture group at index `i`. If
935
935
/// `i` does not correspond to a capture group, or if the capture group
936
936
/// did not participate in the match, then `None` is returned.
937
+ ///
938
+ /// # Examples
939
+ ///
940
+ /// Get the text of the match with a default of an empty string if this
941
+ /// group didn't participate in the match:
942
+ ///
943
+ /// ```rust
944
+ /// # use regex::Regex;
945
+ /// let re = Regex::new(r"[a-z]+(?:([0-9]+)|([A-Z]+))").unwrap();
946
+ /// let caps = re.captures("abc123").unwrap();
947
+ ///
948
+ /// let text1 = caps.get(1).map_or("", |m| m.as_str());
949
+ /// let text2 = caps.get(2).map_or("", |m| m.as_str());
950
+ /// assert_eq!(text1, "123");
951
+ /// assert_eq!(text2, "");
952
+ /// ```
937
953
pub fn get ( & self , i : usize ) -> Option < Match < ' t > > {
938
954
self . locs . pos ( i) . map ( |( s, e) | Match :: new ( self . text , s, e) )
939
955
}
You can’t perform that action at this time.
0 commit comments