@@ -588,6 +588,58 @@ impl Regex {
588
588
) -> CaptureMatches < ' r , ' s > {
589
589
CaptureMatches { re : self , subject, last_end : 0 , last_match : None }
590
590
}
591
+
592
+ /// Replaces the first match in `subject` with the `replacement`,
593
+ /// and puts the replaced string in `output`.
594
+ /// ```rust
595
+ /// # fn example() -> Result<(), ::pcre2::Error> {
596
+ /// use std::str;
597
+ ///
598
+ /// use pcre2::bytes::Regex;
599
+ ///
600
+ /// let re = Regex::new(r"mike")?;
601
+ /// let text = b"Hi mike, wait you are not mike.";
602
+ /// let mut output = Vec::new();
603
+ /// re.substitute(text, b"john", &mut output).unwrap();
604
+ /// assert_eq!(&output, b"Hi john, wait you are not mike.");
605
+ /// # Ok(()) }; example().unwrap()
606
+ /// ```
607
+ pub fn substitute (
608
+ & self ,
609
+ subject : & [ u8 ] ,
610
+ replacement : & [ u8 ] ,
611
+ output : & mut Vec < u8 > ,
612
+ ) -> Result < usize , Error > {
613
+ self . code . substitute ( subject, replacement, output, 0 )
614
+ }
615
+ /// Replaces all the matches in `subject` with the `replacement`,
616
+ /// and puts the replaced string in `output`.
617
+ /// ```rust
618
+ /// # fn example() -> Result<(), ::pcre2::Error> {
619
+ /// use std::str;
620
+ ///
621
+ /// use pcre2::bytes::Regex;
622
+ ///
623
+ /// let re = Regex::new(r"mike")?;
624
+ /// let text = b"Hi mike, wait you are not mike.";
625
+ /// let mut output = Vec::new();
626
+ /// re.substitute_all(text, b"john", &mut output).unwrap();
627
+ /// assert_eq!(&output, b"Hi john, wait you are not john.");
628
+ /// # Ok(()) }; example().unwrap()
629
+ /// ```
630
+ pub fn substitute_all (
631
+ & self ,
632
+ subject : & [ u8 ] ,
633
+ replacement : & [ u8 ] ,
634
+ output : & mut Vec < u8 > ,
635
+ ) -> Result < usize , Error > {
636
+ self . code . substitute (
637
+ subject,
638
+ replacement,
639
+ output,
640
+ pcre2_sys:: PCRE2_SUBSTITUTE_GLOBAL ,
641
+ )
642
+ }
591
643
}
592
644
593
645
/// Advanced or "lower level" search methods.
@@ -1370,4 +1422,28 @@ mod tests {
1370
1422
let matched = re. find ( hay. as_bytes ( ) ) . unwrap ( ) . unwrap ( ) ;
1371
1423
assert_eq ! ( matched. as_bytes( ) , "😀👍🏼🎉" . as_bytes( ) ) ;
1372
1424
}
1425
+ #[ test]
1426
+ fn test_substitute ( ) {
1427
+ let hay = "0123456789abcdefghijklmnopqrstuvwxyzABCDKLMNOPQRSTUVWXYZ" ;
1428
+ let pattern = r"(?i)abcd" ;
1429
+ let re = Regex :: new ( pattern) . unwrap ( ) ;
1430
+ let mut output = Vec :: new ( ) ;
1431
+ re. substitute ( hay. as_bytes ( ) , b"42" , & mut output) . unwrap ( ) ;
1432
+ assert_eq ! (
1433
+ & output,
1434
+ b"012345678942efghijklmnopqrstuvwxyzABCDKLMNOPQRSTUVWXYZ"
1435
+ ) ;
1436
+ }
1437
+ #[ test]
1438
+ fn test_substitute_all ( ) {
1439
+ let hay = "0123456789abcdefghijklmnopqrstuvwxyzABCDKLMNOPQRSTUVWXYZ" ;
1440
+ let pattern = r"(?i)abcd" ;
1441
+ let re = Regex :: new ( pattern) . unwrap ( ) ;
1442
+ let mut output = Vec :: new ( ) ;
1443
+ re. substitute_all ( hay. as_bytes ( ) , b"42" , & mut output) . unwrap ( ) ;
1444
+ assert_eq ! (
1445
+ & output,
1446
+ b"012345678942efghijklmnopqrstuvwxyz42KLMNOPQRSTUVWXYZ"
1447
+ ) ;
1448
+ }
1373
1449
}
0 commit comments