@@ -642,6 +642,91 @@ impl Regex {
642642 SplitN { haystack, it : self . meta . splitn ( haystack, limit) }
643643 }
644644
645+ /// Searches for the first match of this regex in the haystack given, and if
646+ /// found, returns a tuple with the substring preceding the match and the
647+ /// substring following it. If no match is found, then `None` is returned.
648+ ///
649+ /// The matched substring itself is not returned as part of either
650+ /// substring in the returned tuple.
651+ ///
652+ /// # Example
653+ ///
654+ /// To split off the first string delimited by arbitrary amounts of spaces
655+ /// or tabs:
656+ ///
657+ /// ```
658+ /// use regex::Regex;
659+ ///
660+ /// let re = Regex::new(r"[ \t]+").unwrap();
661+ /// let hay = "a b \t c\td e";
662+ /// let got = re.split_once(hay);
663+ /// assert_eq!(got, Some(("a", "b \t c\td e")));
664+ /// ```
665+ ///
666+ /// # Example: more cases
667+ ///
668+ /// Basic usage:
669+ ///
670+ /// ```
671+ /// use regex::Regex;
672+ ///
673+ /// let re = Regex::new(r" ").unwrap();
674+ /// let hay = "Mary had a little lamb";
675+ /// let got = re.split_once(hay);
676+ /// assert_eq!(got, Some(("Mary", "had a little lamb")));
677+ ///
678+ /// let re = Regex::new(r"X").unwrap();
679+ /// let hay = "";
680+ /// let got = re.split_once(hay);
681+ /// assert_eq!(got, None);
682+ ///
683+ /// let re = Regex::new(r"X").unwrap();
684+ /// let hay = "lionXXtigerXleopard";
685+ /// let got = re.split_once(hay);
686+ /// assert_eq!(got, Some(("lion", "XtigerXleopard")));
687+ ///
688+ /// let re = Regex::new(r"::").unwrap();
689+ /// let hay = "lion::tiger::leopard";
690+ /// let got = re.split_once(hay);
691+ /// assert_eq!(got, Some(("lion", "tiger::leopard")));
692+ /// ```
693+ ///
694+ /// If the first match is at the start or end of a haystack, the
695+ /// corresponding substring is empty.
696+ ///
697+ /// ```
698+ /// use regex::Regex;
699+ ///
700+ /// let re = Regex::new(r"0").unwrap();
701+ /// let hay = "01";
702+ /// let got = re.split_once(hay);
703+ /// assert_eq!(got, Some(("", "1")));
704+ ///
705+ /// let re = Regex::new(r"0").unwrap();
706+ /// let hay = "10";
707+ /// let got = re.split_once(hay);
708+ /// assert_eq!(got, Some(("1", "")));
709+ /// ```
710+ ///
711+ /// When the empty string is used as a regex, the resulting tuple consists
712+ /// of the empty string followed by the complete haystack:
713+ ///
714+ /// ```
715+ /// use regex::Regex;
716+ ///
717+ /// let re = Regex::new(r"").unwrap();
718+ /// let hay = "rust";
719+ /// let got = re.split_once(hay);
720+ /// assert_eq!(got, Some(("", "rust")));
721+ /// ```
722+ #[ inline]
723+ pub fn split_once < ' h > ( & self , haystack : & ' h [ u8 ] ) -> Option < ( & ' h [ u8 ] , & ' h [ u8 ] ) > {
724+ let m = self . find ( haystack) ?;
725+ let preceding = & haystack[ ..m. start ( ) ] ;
726+ let succeeding = & haystack[ m. end ( ) ..] ;
727+ Some ( ( preceding, succeeding) )
728+ }
729+
645730 /// Replaces the leftmost-first match in the given haystack with the
646731 /// replacement provided. The replacement can be a regular string (where
647732 /// `$N` and `$name` are expanded to match capture groups) or a function
0 commit comments