regex/regex/bytes.rs
1use alloc::{borrow::Cow, string::String, sync::Arc, vec::Vec};
2
3use regex_automata::{meta, util::captures, Input, PatternID};
4
5use crate::{bytes::RegexBuilder, error::Error};
6
7/// A convenient way to construct regex patterns from string literals.
8///
9/// This macro can be used to construct reusable instances of [`Regex`] with
10/// reduced boilerplate. The constructed `Regex` is stored in a static so the
11/// pattern is compiled approximately once, even when called multiple times.
12///
13/// There is *no compile-time checking of patterns* with `regex!`. Instead,
14/// invalid patterns will panic the first time the regex is used. Invalid
15/// patterns should still not be used with `regex!`; if compile-time checking
16/// becomes feasible in the future, it may be added within a non-semver-breaking
17/// release. In the meantime, consider enabling [`clippy::invalid_regex`].
18///
19/// # Examples
20///
21/// ```
22/// use regex::bytes::{Regex, regex};
23///
24/// assert!(regex!("[a-z]").is_match(b"a"));
25/// assert!(regex!("(inconceivable!|classic blunder)").is_match(b"inconceivable!"));
26///
27/// let re: &Regex = regex!(r"(\d{3})-(\d{4})");
28/// assert_eq!(&re.captures(b"867-5309").unwrap()[1], b"867");
29/// ```
30///
31/// An invalid pattern will panic when it is first used:
32///
33/// ```should_panic
34/// use regex::bytes::regex;
35///
36/// let re = regex!("invalid -> ("); // no panic here
37/// re.is_match(b"invalid -> ("); // panic!
38/// ```
39///
40/// [`clippy::invalid_regex`]: https://rust-lang.github.io/rust-clippy/master/#invalid_regex
41// `macro_export` always makes the macro available at crate root. We hide
42// from documentation there and instead re-export it in `crate::bytes` to get
43// the desired behavior.
44#[macro_export]
45#[doc(hidden)]
46macro_rules! __bytes_regex {
47 ($re:literal) => {{
48 static REGEX: $crate::__private::Lazy<$crate::bytes::Regex> =
49 $crate::__private::Lazy::new(|| {
50 $crate::bytes::Regex::new($re).expect("invalid regex pattern")
51 });
52
53 // Coerce returned type from `&Lazy<Regex>` to `&Regex` to avoid making the
54 // inner type public.
55 let re: &$crate::bytes::Regex = ®EX;
56 re
57 }};
58}
59
60/// A compiled regular expression for searching Unicode haystacks.
61///
62/// A `Regex` can be used to search haystacks, split haystacks into substrings
63/// or replace substrings in a haystack with a different substring. All
64/// searching is done with an implicit `(?s:.)*?` at the beginning and end of
65/// an pattern. To force an expression to match the whole string (or a prefix
66/// or a suffix), you must use an anchor like `^` or `$` (or `\A` and `\z`).
67///
68/// Like the `Regex` type in the parent module, matches with this regex return
69/// byte offsets into the haystack. **Unlike** the parent `Regex` type, these
70/// byte offsets may not correspond to UTF-8 sequence boundaries since the
71/// regexes in this module can match arbitrary bytes.
72///
73/// The only methods that allocate new byte strings are the string replacement
74/// methods. All other methods (searching and splitting) return borrowed
75/// references into the haystack given.
76///
77/// # Example
78///
79/// Find the offsets of a US phone number:
80///
81/// ```
82/// use regex::bytes::Regex;
83///
84/// let re = Regex::new("[0-9]{3}-[0-9]{3}-[0-9]{4}").unwrap();
85/// let m = re.find(b"phone: 111-222-3333").unwrap();
86/// assert_eq!(7..19, m.range());
87/// ```
88///
89/// # Example: extracting capture groups
90///
91/// A common way to use regexes is with capture groups. That is, instead of
92/// just looking for matches of an entire regex, parentheses are used to create
93/// groups that represent part of the match.
94///
95/// For example, consider a haystack with multiple lines, and each line has
96/// three whitespace delimited fields where the second field is expected to be
97/// a number and the third field a boolean. To make this convenient, we use
98/// the [`Captures::extract`] API to put the strings that match each group
99/// into a fixed size array:
100///
101/// ```
102/// use regex::bytes::Regex;
103///
104/// let hay = b"
105/// rabbit 54 true
106/// groundhog 2 true
107/// does not match
108/// fox 109 false
109/// ";
110/// let re = Regex::new(r"(?m)^\s*(\S+)\s+([0-9]+)\s+(true|false)\s*$").unwrap();
111/// let mut fields: Vec<(&[u8], i64, bool)> = vec![];
112/// for (_, [f1, f2, f3]) in re.captures_iter(hay).map(|caps| caps.extract()) {
113/// // These unwraps are OK because our pattern is written in a way where
114/// // all matches for f2 and f3 will be valid UTF-8.
115/// let f2 = std::str::from_utf8(f2).unwrap();
116/// let f3 = std::str::from_utf8(f3).unwrap();
117/// fields.push((f1, f2.parse()?, f3.parse()?));
118/// }
119/// assert_eq!(fields, vec![
120/// (&b"rabbit"[..], 54, true),
121/// (&b"groundhog"[..], 2, true),
122/// (&b"fox"[..], 109, false),
123/// ]);
124///
125/// # Ok::<(), Box<dyn std::error::Error>>(())
126/// ```
127///
128/// # Example: matching invalid UTF-8
129///
130/// One of the reasons for searching `&[u8]` haystacks is that the `&[u8]`
131/// might not be valid UTF-8. Indeed, with a `bytes::Regex`, patterns that
132/// match invalid UTF-8 are explicitly allowed. Here's one example that looks
133/// for valid UTF-8 fields that might be separated by invalid UTF-8. In this
134/// case, we use `(?s-u:.)`, which matches any byte. Attempting to use it in a
135/// top-level `Regex` will result in the regex failing to compile. Notice also
136/// that we use `.` with Unicode mode enabled, in which case, only valid UTF-8
137/// is matched. In this way, we can build one pattern where some parts only
138/// match valid UTF-8 while other parts are more permissive.
139///
140/// ```
141/// use regex::bytes::Regex;
142///
143/// // F0 9F 92 A9 is the UTF-8 encoding for a Pile of Poo.
144/// let hay = b"\xFF\xFFfoo\xFF\xFF\xFF\xF0\x9F\x92\xA9\xFF";
145/// // An equivalent to '(?s-u:.)' is '(?-u:[\x00-\xFF])'.
146/// let re = Regex::new(r"(?s)(?-u:.)*?(?<f1>.+)(?-u:.)*?(?<f2>.+)").unwrap();
147/// let caps = re.captures(hay).unwrap();
148/// assert_eq!(&caps["f1"], &b"foo"[..]);
149/// assert_eq!(&caps["f2"], "💩".as_bytes());
150/// ```
151#[derive(Clone)]
152pub struct Regex {
153 pub(crate) meta: meta::Regex,
154 pub(crate) pattern: Arc<str>,
155}
156
157impl core::fmt::Display for Regex {
158 /// Shows the original regular expression.
159 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
160 write!(f, "{}", self.as_str())
161 }
162}
163
164impl core::fmt::Debug for Regex {
165 /// Shows the original regular expression.
166 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
167 f.debug_tuple("Regex").field(&self.as_str()).finish()
168 }
169}
170
171impl core::str::FromStr for Regex {
172 type Err = Error;
173
174 /// Attempts to parse a string into a regular expression
175 fn from_str(s: &str) -> Result<Regex, Error> {
176 Regex::new(s)
177 }
178}
179
180impl TryFrom<&str> for Regex {
181 type Error = Error;
182
183 /// Attempts to parse a string into a regular expression
184 fn try_from(s: &str) -> Result<Regex, Error> {
185 Regex::new(s)
186 }
187}
188
189impl TryFrom<String> for Regex {
190 type Error = Error;
191
192 /// Attempts to parse a string into a regular expression
193 fn try_from(s: String) -> Result<Regex, Error> {
194 Regex::new(&s)
195 }
196}
197
198/// Core regular expression methods.
199impl Regex {
200 /// Compiles a regular expression. Once compiled, it can be used repeatedly
201 /// to search, split or replace substrings in a haystack.
202 ///
203 /// Note that regex compilation tends to be a somewhat expensive process,
204 /// and unlike higher level environments, compilation is not automatically
205 /// cached for you. One should endeavor to compile a regex once and then
206 /// reuse it. For example, it's a bad idea to compile the same regex
207 /// repeatedly in a loop.
208 ///
209 /// # Errors
210 ///
211 /// If an invalid pattern is given, then an error is returned.
212 /// An error is also returned if the pattern is valid, but would
213 /// produce a regex that is bigger than the configured size limit via
214 /// [`RegexBuilder::size_limit`]. (A reasonable size limit is enabled by
215 /// default.)
216 ///
217 /// # Example
218 ///
219 /// ```
220 /// use regex::bytes::Regex;
221 ///
222 /// // An Invalid pattern because of an unclosed parenthesis
223 /// assert!(Regex::new(r"foo(bar").is_err());
224 /// // An invalid pattern because the regex would be too big
225 /// // because Unicode tends to inflate things.
226 /// assert!(Regex::new(r"\w{1000}").is_err());
227 /// // Disabling Unicode can make the regex much smaller,
228 /// // potentially by up to or more than an order of magnitude.
229 /// assert!(Regex::new(r"(?-u:\w){1000}").is_ok());
230 /// ```
231 pub fn new(re: &str) -> Result<Regex, Error> {
232 RegexBuilder::new(re).build()
233 }
234
235 /// Returns true if and only if there is a match for the regex anywhere
236 /// in the haystack given.
237 ///
238 /// It is recommended to use this method if all you need to do is test
239 /// whether a match exists, since the underlying matching engine may be
240 /// able to do less work.
241 ///
242 /// # Example
243 ///
244 /// Test if some haystack contains at least one word with exactly 13
245 /// Unicode word characters:
246 ///
247 /// ```
248 /// use regex::bytes::Regex;
249 ///
250 /// let re = Regex::new(r"\b\w{13}\b").unwrap();
251 /// let hay = b"I categorically deny having triskaidekaphobia.";
252 /// assert!(re.is_match(hay));
253 /// ```
254 #[inline]
255 pub fn is_match(&self, haystack: &[u8]) -> bool {
256 self.is_match_at(haystack, 0)
257 }
258
259 /// This routine searches for the first match of this regex in the
260 /// haystack given, and if found, returns a [`Match`]. The `Match`
261 /// provides access to both the byte offsets of the match and the actual
262 /// substring that matched.
263 ///
264 /// Note that this should only be used if you want to find the entire
265 /// match. If instead you just want to test the existence of a match,
266 /// it's potentially faster to use `Regex::is_match(hay)` instead of
267 /// `Regex::find(hay).is_some()`.
268 ///
269 /// # Example
270 ///
271 /// Find the first word with exactly 13 Unicode word characters:
272 ///
273 /// ```
274 /// use regex::bytes::Regex;
275 ///
276 /// let re = Regex::new(r"\b\w{13}\b").unwrap();
277 /// let hay = b"I categorically deny having triskaidekaphobia.";
278 /// let mat = re.find(hay).unwrap();
279 /// assert_eq!(2..15, mat.range());
280 /// assert_eq!(b"categorically", mat.as_bytes());
281 /// ```
282 #[inline]
283 pub fn find<'h>(&self, haystack: &'h [u8]) -> Option<Match<'h>> {
284 self.find_at(haystack, 0)
285 }
286
287 /// Returns an iterator that yields successive non-overlapping matches in
288 /// the given haystack. The iterator yields values of type [`Match`].
289 ///
290 /// # Time complexity
291 ///
292 /// Note that since `find_iter` runs potentially many searches on the
293 /// haystack and since each search has worst case `O(m * n)` time
294 /// complexity, the overall worst case time complexity for iteration is
295 /// `O(m * n^2)`.
296 ///
297 /// # Example
298 ///
299 /// Find every word with exactly 13 Unicode word characters:
300 ///
301 /// ```
302 /// use regex::bytes::Regex;
303 ///
304 /// let re = Regex::new(r"\b\w{13}\b").unwrap();
305 /// let hay = b"Retroactively relinquishing remunerations is reprehensible.";
306 /// let matches: Vec<_> = re.find_iter(hay).map(|m| m.as_bytes()).collect();
307 /// assert_eq!(matches, vec![
308 /// &b"Retroactively"[..],
309 /// &b"relinquishing"[..],
310 /// &b"remunerations"[..],
311 /// &b"reprehensible"[..],
312 /// ]);
313 /// ```
314 #[inline]
315 pub fn find_iter<'r, 'h>(&'r self, haystack: &'h [u8]) -> Matches<'r, 'h> {
316 Matches { haystack, it: self.meta.find_iter(haystack) }
317 }
318
319 /// This routine searches for the first match of this regex in the haystack
320 /// given, and if found, returns not only the overall match but also the
321 /// matches of each capture group in the regex. If no match is found, then
322 /// `None` is returned.
323 ///
324 /// Capture group `0` always corresponds to an implicit unnamed group that
325 /// includes the entire match. If a match is found, this group is always
326 /// present. Subsequent groups may be named and are numbered, starting
327 /// at 1, by the order in which the opening parenthesis appears in the
328 /// pattern. For example, in the pattern `(?<a>.(?<b>.))(?<c>.)`, `a`,
329 /// `b` and `c` correspond to capture group indices `1`, `2` and `3`,
330 /// respectively.
331 ///
332 /// You should only use `captures` if you need access to the capture group
333 /// matches. Otherwise, [`Regex::find`] is generally faster for discovering
334 /// just the overall match.
335 ///
336 /// # Example
337 ///
338 /// Say you have some haystack with movie names and their release years,
339 /// like "'Citizen Kane' (1941)". It'd be nice if we could search for
340 /// strings looking like that, while also extracting the movie name and its
341 /// release year separately. The example below shows how to do that.
342 ///
343 /// ```
344 /// use regex::bytes::Regex;
345 ///
346 /// let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
347 /// let hay = b"Not my favorite movie: 'Citizen Kane' (1941).";
348 /// let caps = re.captures(hay).unwrap();
349 /// assert_eq!(caps.get(0).unwrap().as_bytes(), b"'Citizen Kane' (1941)");
350 /// assert_eq!(caps.get(1).unwrap().as_bytes(), b"Citizen Kane");
351 /// assert_eq!(caps.get(2).unwrap().as_bytes(), b"1941");
352 /// // You can also access the groups by index using the Index notation.
353 /// // Note that this will panic on an invalid index. In this case, these
354 /// // accesses are always correct because the overall regex will only
355 /// // match when these capture groups match.
356 /// assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
357 /// assert_eq!(&caps[1], b"Citizen Kane");
358 /// assert_eq!(&caps[2], b"1941");
359 /// ```
360 ///
361 /// Note that the full match is at capture group `0`. Each subsequent
362 /// capture group is indexed by the order of its opening `(`.
363 ///
364 /// We can make this example a bit clearer by using *named* capture groups:
365 ///
366 /// ```
367 /// use regex::bytes::Regex;
368 ///
369 /// let re = Regex::new(r"'(?<title>[^']+)'\s+\((?<year>\d{4})\)").unwrap();
370 /// let hay = b"Not my favorite movie: 'Citizen Kane' (1941).";
371 /// let caps = re.captures(hay).unwrap();
372 /// assert_eq!(caps.get(0).unwrap().as_bytes(), b"'Citizen Kane' (1941)");
373 /// assert_eq!(caps.name("title").unwrap().as_bytes(), b"Citizen Kane");
374 /// assert_eq!(caps.name("year").unwrap().as_bytes(), b"1941");
375 /// // You can also access the groups by name using the Index notation.
376 /// // Note that this will panic on an invalid group name. In this case,
377 /// // these accesses are always correct because the overall regex will
378 /// // only match when these capture groups match.
379 /// assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
380 /// assert_eq!(&caps["title"], b"Citizen Kane");
381 /// assert_eq!(&caps["year"], b"1941");
382 /// ```
383 ///
384 /// Here we name the capture groups, which we can access with the `name`
385 /// method or the `Index` notation with a `&str`. Note that the named
386 /// capture groups are still accessible with `get` or the `Index` notation
387 /// with a `usize`.
388 ///
389 /// The `0`th capture group is always unnamed, so it must always be
390 /// accessed with `get(0)` or `[0]`.
391 ///
392 /// Finally, one other way to get the matched substrings is with the
393 /// [`Captures::extract`] API:
394 ///
395 /// ```
396 /// use regex::bytes::Regex;
397 ///
398 /// let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
399 /// let hay = b"Not my favorite movie: 'Citizen Kane' (1941).";
400 /// let (full, [title, year]) = re.captures(hay).unwrap().extract();
401 /// assert_eq!(full, b"'Citizen Kane' (1941)");
402 /// assert_eq!(title, b"Citizen Kane");
403 /// assert_eq!(year, b"1941");
404 /// ```
405 #[inline]
406 pub fn captures<'h>(&self, haystack: &'h [u8]) -> Option<Captures<'h>> {
407 self.captures_at(haystack, 0)
408 }
409
410 /// Returns an iterator that yields successive non-overlapping matches in
411 /// the given haystack. The iterator yields values of type [`Captures`].
412 ///
413 /// This is the same as [`Regex::find_iter`], but instead of only providing
414 /// access to the overall match, each value yield includes access to the
415 /// matches of all capture groups in the regex. Reporting this extra match
416 /// data is potentially costly, so callers should only use `captures_iter`
417 /// over `find_iter` when they actually need access to the capture group
418 /// matches.
419 ///
420 /// # Time complexity
421 ///
422 /// Note that since `captures_iter` runs potentially many searches on the
423 /// haystack and since each search has worst case `O(m * n)` time
424 /// complexity, the overall worst case time complexity for iteration is
425 /// `O(m * n^2)`.
426 ///
427 /// # Example
428 ///
429 /// We can use this to find all movie titles and their release years in
430 /// some haystack, where the movie is formatted like "'Title' (xxxx)":
431 ///
432 /// ```
433 /// use regex::bytes::Regex;
434 ///
435 /// let re = Regex::new(r"'([^']+)'\s+\(([0-9]{4})\)").unwrap();
436 /// let hay = b"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
437 /// let mut movies = vec![];
438 /// for (_, [title, year]) in re.captures_iter(hay).map(|c| c.extract()) {
439 /// // OK because [0-9]{4} can only match valid UTF-8.
440 /// let year = std::str::from_utf8(year).unwrap();
441 /// movies.push((title, year.parse::<i64>()?));
442 /// }
443 /// assert_eq!(movies, vec![
444 /// (&b"Citizen Kane"[..], 1941),
445 /// (&b"The Wizard of Oz"[..], 1939),
446 /// (&b"M"[..], 1931),
447 /// ]);
448 /// # Ok::<(), Box<dyn std::error::Error>>(())
449 /// ```
450 ///
451 /// Or with named groups:
452 ///
453 /// ```
454 /// use regex::bytes::Regex;
455 ///
456 /// let re = Regex::new(r"'(?<title>[^']+)'\s+\((?<year>[0-9]{4})\)").unwrap();
457 /// let hay = b"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
458 /// let mut it = re.captures_iter(hay);
459 ///
460 /// let caps = it.next().unwrap();
461 /// assert_eq!(&caps["title"], b"Citizen Kane");
462 /// assert_eq!(&caps["year"], b"1941");
463 ///
464 /// let caps = it.next().unwrap();
465 /// assert_eq!(&caps["title"], b"The Wizard of Oz");
466 /// assert_eq!(&caps["year"], b"1939");
467 ///
468 /// let caps = it.next().unwrap();
469 /// assert_eq!(&caps["title"], b"M");
470 /// assert_eq!(&caps["year"], b"1931");
471 /// ```
472 #[inline]
473 pub fn captures_iter<'r, 'h>(
474 &'r self,
475 haystack: &'h [u8],
476 ) -> CaptureMatches<'r, 'h> {
477 CaptureMatches { haystack, it: self.meta.captures_iter(haystack) }
478 }
479
480 /// Returns an iterator of substrings of the haystack given, delimited by a
481 /// match of the regex. Namely, each element of the iterator corresponds to
482 /// a part of the haystack that *isn't* matched by the regular expression.
483 ///
484 /// # Time complexity
485 ///
486 /// Since iterators over all matches requires running potentially many
487 /// searches on the haystack, and since each search has worst case
488 /// `O(m * n)` time complexity, the overall worst case time complexity for
489 /// this routine is `O(m * n^2)`.
490 ///
491 /// # Example
492 ///
493 /// To split a string delimited by arbitrary amounts of spaces or tabs:
494 ///
495 /// ```
496 /// use regex::bytes::Regex;
497 ///
498 /// let re = Regex::new(r"[ \t]+").unwrap();
499 /// let hay = b"a b \t c\td e";
500 /// let fields: Vec<&[u8]> = re.split(hay).collect();
501 /// assert_eq!(fields, vec![
502 /// &b"a"[..], &b"b"[..], &b"c"[..], &b"d"[..], &b"e"[..],
503 /// ]);
504 /// ```
505 ///
506 /// # Example: more cases
507 ///
508 /// Basic usage:
509 ///
510 /// ```
511 /// use regex::bytes::Regex;
512 ///
513 /// let re = Regex::new(r" ").unwrap();
514 /// let hay = b"Mary had a little lamb";
515 /// let got: Vec<&[u8]> = re.split(hay).collect();
516 /// assert_eq!(got, vec![
517 /// &b"Mary"[..], &b"had"[..], &b"a"[..], &b"little"[..], &b"lamb"[..],
518 /// ]);
519 ///
520 /// let re = Regex::new(r"X").unwrap();
521 /// let hay = b"";
522 /// let got: Vec<&[u8]> = re.split(hay).collect();
523 /// assert_eq!(got, vec![&b""[..]]);
524 ///
525 /// let re = Regex::new(r"X").unwrap();
526 /// let hay = b"lionXXtigerXleopard";
527 /// let got: Vec<&[u8]> = re.split(hay).collect();
528 /// assert_eq!(got, vec![
529 /// &b"lion"[..], &b""[..], &b"tiger"[..], &b"leopard"[..],
530 /// ]);
531 ///
532 /// let re = Regex::new(r"::").unwrap();
533 /// let hay = b"lion::tiger::leopard";
534 /// let got: Vec<&[u8]> = re.split(hay).collect();
535 /// assert_eq!(got, vec![&b"lion"[..], &b"tiger"[..], &b"leopard"[..]]);
536 /// ```
537 ///
538 /// If a haystack contains multiple contiguous matches, you will end up
539 /// with empty spans yielded by the iterator:
540 ///
541 /// ```
542 /// use regex::bytes::Regex;
543 ///
544 /// let re = Regex::new(r"X").unwrap();
545 /// let hay = b"XXXXaXXbXc";
546 /// let got: Vec<&[u8]> = re.split(hay).collect();
547 /// assert_eq!(got, vec![
548 /// &b""[..], &b""[..], &b""[..], &b""[..],
549 /// &b"a"[..], &b""[..], &b"b"[..], &b"c"[..],
550 /// ]);
551 ///
552 /// let re = Regex::new(r"/").unwrap();
553 /// let hay = b"(///)";
554 /// let got: Vec<&[u8]> = re.split(hay).collect();
555 /// assert_eq!(got, vec![&b"("[..], &b""[..], &b""[..], &b")"[..]]);
556 /// ```
557 ///
558 /// Separators at the start or end of a haystack are neighbored by empty
559 /// substring.
560 ///
561 /// ```
562 /// use regex::bytes::Regex;
563 ///
564 /// let re = Regex::new(r"0").unwrap();
565 /// let hay = b"010";
566 /// let got: Vec<&[u8]> = re.split(hay).collect();
567 /// assert_eq!(got, vec![&b""[..], &b"1"[..], &b""[..]]);
568 /// ```
569 ///
570 /// When the regex can match the empty string, it splits at every byte
571 /// position in the haystack. This includes between all UTF-8 code units.
572 /// (The top-level [`Regex::split`](crate::Regex::split) will only split
573 /// at valid UTF-8 boundaries.)
574 ///
575 /// ```
576 /// use regex::bytes::Regex;
577 ///
578 /// let re = Regex::new(r"").unwrap();
579 /// let hay = "☃".as_bytes();
580 /// let got: Vec<&[u8]> = re.split(hay).collect();
581 /// assert_eq!(got, vec![
582 /// &[][..], &[b'\xE2'][..], &[b'\x98'][..], &[b'\x83'][..], &[][..],
583 /// ]);
584 /// ```
585 ///
586 /// Contiguous separators (commonly shows up with whitespace), can lead to
587 /// possibly surprising behavior. For example, this code is correct:
588 ///
589 /// ```
590 /// use regex::bytes::Regex;
591 ///
592 /// let re = Regex::new(r" ").unwrap();
593 /// let hay = b" a b c";
594 /// let got: Vec<&[u8]> = re.split(hay).collect();
595 /// assert_eq!(got, vec![
596 /// &b""[..], &b""[..], &b""[..], &b""[..],
597 /// &b"a"[..], &b""[..], &b"b"[..], &b"c"[..],
598 /// ]);
599 /// ```
600 ///
601 /// It does *not* give you `["a", "b", "c"]`. For that behavior, you'd want
602 /// to match contiguous space characters:
603 ///
604 /// ```
605 /// use regex::bytes::Regex;
606 ///
607 /// let re = Regex::new(r" +").unwrap();
608 /// let hay = b" a b c";
609 /// let got: Vec<&[u8]> = re.split(hay).collect();
610 /// // N.B. This does still include a leading empty span because ' +'
611 /// // matches at the beginning of the haystack.
612 /// assert_eq!(got, vec![&b""[..], &b"a"[..], &b"b"[..], &b"c"[..]]);
613 /// ```
614 #[inline]
615 pub fn split<'r, 'h>(&'r self, haystack: &'h [u8]) -> Split<'r, 'h> {
616 Split { haystack, it: self.meta.split(haystack) }
617 }
618
619 /// Returns an iterator of at most `limit` substrings of the haystack
620 /// given, delimited by a match of the regex. (A `limit` of `0` will return
621 /// no substrings.) Namely, each element of the iterator corresponds to a
622 /// part of the haystack that *isn't* matched by the regular expression.
623 /// The remainder of the haystack that is not split will be the last
624 /// element in the iterator.
625 ///
626 /// # Time complexity
627 ///
628 /// Since iterators over all matches requires running potentially many
629 /// searches on the haystack, and since each search has worst case
630 /// `O(m * n)` time complexity, the overall worst case time complexity for
631 /// this routine is `O(m * n^2)`.
632 ///
633 /// Although note that the worst case time here has an upper bound given
634 /// by the `limit` parameter.
635 ///
636 /// # Example
637 ///
638 /// Get the first two words in some haystack:
639 ///
640 /// ```
641 /// use regex::bytes::Regex;
642 ///
643 /// let re = Regex::new(r"\W+").unwrap();
644 /// let hay = b"Hey! How are you?";
645 /// let fields: Vec<&[u8]> = re.splitn(hay, 3).collect();
646 /// assert_eq!(fields, vec![&b"Hey"[..], &b"How"[..], &b"are you?"[..]]);
647 /// ```
648 ///
649 /// # Examples: more cases
650 ///
651 /// ```
652 /// use regex::bytes::Regex;
653 ///
654 /// let re = Regex::new(r" ").unwrap();
655 /// let hay = b"Mary had a little lamb";
656 /// let got: Vec<&[u8]> = re.splitn(hay, 3).collect();
657 /// assert_eq!(got, vec![&b"Mary"[..], &b"had"[..], &b"a little lamb"[..]]);
658 ///
659 /// let re = Regex::new(r"X").unwrap();
660 /// let hay = b"";
661 /// let got: Vec<&[u8]> = re.splitn(hay, 3).collect();
662 /// assert_eq!(got, vec![&b""[..]]);
663 ///
664 /// let re = Regex::new(r"X").unwrap();
665 /// let hay = b"lionXXtigerXleopard";
666 /// let got: Vec<&[u8]> = re.splitn(hay, 3).collect();
667 /// assert_eq!(got, vec![&b"lion"[..], &b""[..], &b"tigerXleopard"[..]]);
668 ///
669 /// let re = Regex::new(r"::").unwrap();
670 /// let hay = b"lion::tiger::leopard";
671 /// let got: Vec<&[u8]> = re.splitn(hay, 2).collect();
672 /// assert_eq!(got, vec![&b"lion"[..], &b"tiger::leopard"[..]]);
673 ///
674 /// let re = Regex::new(r"X").unwrap();
675 /// let hay = b"abcXdef";
676 /// let got: Vec<&[u8]> = re.splitn(hay, 1).collect();
677 /// assert_eq!(got, vec![&b"abcXdef"[..]]);
678 ///
679 /// let re = Regex::new(r"X").unwrap();
680 /// let hay = b"abcdef";
681 /// let got: Vec<&[u8]> = re.splitn(hay, 2).collect();
682 /// assert_eq!(got, vec![&b"abcdef"[..]]);
683 ///
684 /// let re = Regex::new(r"X").unwrap();
685 /// let hay = b"abcXdef";
686 /// let got: Vec<&[u8]> = re.splitn(hay, 0).collect();
687 /// assert!(got.is_empty());
688 /// ```
689 #[inline]
690 pub fn splitn<'r, 'h>(
691 &'r self,
692 haystack: &'h [u8],
693 limit: usize,
694 ) -> SplitN<'r, 'h> {
695 SplitN { haystack, it: self.meta.splitn(haystack, limit) }
696 }
697
698 /// Replaces the leftmost-first match in the given haystack with the
699 /// replacement provided. The replacement can be a regular string (where
700 /// `$N` and `$name` are expanded to match capture groups) or a function
701 /// that takes a [`Captures`] and returns the replaced string.
702 ///
703 /// If no match is found, then the haystack is returned unchanged. In that
704 /// case, this implementation will likely return a `Cow::Borrowed` value
705 /// such that no allocation is performed.
706 ///
707 /// When a `Cow::Borrowed` is returned, the value returned is guaranteed
708 /// to be equivalent to the `haystack` given.
709 ///
710 /// # Replacement string syntax
711 ///
712 /// All instances of `$ref` in the replacement string are replaced with
713 /// the substring corresponding to the capture group identified by `ref`.
714 ///
715 /// `ref` may be an integer corresponding to the index of the capture group
716 /// (counted by order of opening parenthesis where `0` is the entire match)
717 /// or it can be a name (consisting of letters, digits or underscores)
718 /// corresponding to a named capture group.
719 ///
720 /// If `ref` isn't a valid capture group (whether the name doesn't exist or
721 /// isn't a valid index), then it is replaced with the empty string.
722 ///
723 /// The longest possible name is used. For example, `$1a` looks up the
724 /// capture group named `1a` and not the capture group at index `1`. To
725 /// exert more precise control over the name, use braces, e.g., `${1}a`.
726 ///
727 /// To write a literal `$` use `$$`.
728 ///
729 /// # Example
730 ///
731 /// Note that this function is polymorphic with respect to the replacement.
732 /// In typical usage, this can just be a normal string:
733 ///
734 /// ```
735 /// use regex::bytes::Regex;
736 ///
737 /// let re = Regex::new(r"[^01]+").unwrap();
738 /// assert_eq!(re.replace(b"1078910", b""), &b"1010"[..]);
739 /// ```
740 ///
741 /// But anything satisfying the [`Replacer`] trait will work. For example,
742 /// a closure of type `|&Captures| -> String` provides direct access to the
743 /// captures corresponding to a match. This allows one to access capturing
744 /// group matches easily:
745 ///
746 /// ```
747 /// use regex::bytes::{Captures, Regex};
748 ///
749 /// let re = Regex::new(r"([^,\s]+),\s+(\S+)").unwrap();
750 /// let result = re.replace(b"Springsteen, Bruce", |caps: &Captures| {
751 /// let mut buf = vec![];
752 /// buf.extend_from_slice(&caps[2]);
753 /// buf.push(b' ');
754 /// buf.extend_from_slice(&caps[1]);
755 /// buf
756 /// });
757 /// assert_eq!(result, &b"Bruce Springsteen"[..]);
758 /// ```
759 ///
760 /// But this is a bit cumbersome to use all the time. Instead, a simple
761 /// syntax is supported (as described above) that expands `$name` into the
762 /// corresponding capture group. Here's the last example, but using this
763 /// expansion technique with named capture groups:
764 ///
765 /// ```
766 /// use regex::bytes::Regex;
767 ///
768 /// let re = Regex::new(r"(?<last>[^,\s]+),\s+(?<first>\S+)").unwrap();
769 /// let result = re.replace(b"Springsteen, Bruce", b"$first $last");
770 /// assert_eq!(result, &b"Bruce Springsteen"[..]);
771 /// ```
772 ///
773 /// Note that using `$2` instead of `$first` or `$1` instead of `$last`
774 /// would produce the same result. To write a literal `$` use `$$`.
775 ///
776 /// Sometimes the replacement string requires use of curly braces to
777 /// delineate a capture group replacement when it is adjacent to some other
778 /// literal text. For example, if we wanted to join two words together with
779 /// an underscore:
780 ///
781 /// ```
782 /// use regex::bytes::Regex;
783 ///
784 /// let re = Regex::new(r"(?<first>\w+)\s+(?<second>\w+)").unwrap();
785 /// let result = re.replace(b"deep fried", b"${first}_$second");
786 /// assert_eq!(result, &b"deep_fried"[..]);
787 /// ```
788 ///
789 /// Without the curly braces, the capture group name `first_` would be
790 /// used, and since it doesn't exist, it would be replaced with the empty
791 /// string.
792 ///
793 /// Finally, sometimes you just want to replace a literal string with no
794 /// regard for capturing group expansion. This can be done by wrapping a
795 /// string with [`NoExpand`]:
796 ///
797 /// ```
798 /// use regex::bytes::{NoExpand, Regex};
799 ///
800 /// let re = Regex::new(r"(?<last>[^,\s]+),\s+(\S+)").unwrap();
801 /// let result = re.replace(b"Springsteen, Bruce", NoExpand(b"$2 $last"));
802 /// assert_eq!(result, &b"$2 $last"[..]);
803 /// ```
804 ///
805 /// Using `NoExpand` may also be faster, since the replacement string won't
806 /// need to be parsed for the `$` syntax.
807 #[inline]
808 pub fn replace<'h, R: Replacer>(
809 &self,
810 haystack: &'h [u8],
811 rep: R,
812 ) -> Cow<'h, [u8]> {
813 self.replacen(haystack, 1, rep)
814 }
815
816 /// Replaces all non-overlapping matches in the haystack with the
817 /// replacement provided. This is the same as calling `replacen` with
818 /// `limit` set to `0`.
819 ///
820 /// If no match is found, then the haystack is returned unchanged. In that
821 /// case, this implementation will likely return a `Cow::Borrowed` value
822 /// such that no allocation is performed.
823 ///
824 /// When a `Cow::Borrowed` is returned, the value returned is guaranteed
825 /// to be equivalent to the `haystack` given.
826 ///
827 /// The documentation for [`Regex::replace`] goes into more detail about
828 /// what kinds of replacement strings are supported.
829 ///
830 /// # Time complexity
831 ///
832 /// Since iterators over all matches requires running potentially many
833 /// searches on the haystack, and since each search has worst case
834 /// `O(m * n)` time complexity, the overall worst case time complexity for
835 /// this routine is `O(m * n^2)`.
836 ///
837 /// # Fallibility
838 ///
839 /// If you need to write a replacement routine where any individual
840 /// replacement might "fail," doing so with this API isn't really feasible
841 /// because there's no way to stop the search process if a replacement
842 /// fails. Instead, if you need this functionality, you should consider
843 /// implementing your own replacement routine:
844 ///
845 /// ```
846 /// use regex::bytes::{Captures, Regex};
847 ///
848 /// fn replace_all<E>(
849 /// re: &Regex,
850 /// haystack: &[u8],
851 /// replacement: impl Fn(&Captures) -> Result<Vec<u8>, E>,
852 /// ) -> Result<Vec<u8>, E> {
853 /// let mut new = Vec::with_capacity(haystack.len());
854 /// let mut last_match = 0;
855 /// for caps in re.captures_iter(haystack) {
856 /// let m = caps.get(0).unwrap();
857 /// new.extend_from_slice(&haystack[last_match..m.start()]);
858 /// new.extend_from_slice(&replacement(&caps)?);
859 /// last_match = m.end();
860 /// }
861 /// new.extend_from_slice(&haystack[last_match..]);
862 /// Ok(new)
863 /// }
864 ///
865 /// // Let's replace each word with the number of bytes in that word.
866 /// // But if we see a word that is "too long," we'll give up.
867 /// let re = Regex::new(r"\w+").unwrap();
868 /// let replacement = |caps: &Captures| -> Result<Vec<u8>, &'static str> {
869 /// if caps[0].len() >= 5 {
870 /// return Err("word too long");
871 /// }
872 /// Ok(caps[0].len().to_string().into_bytes())
873 /// };
874 /// assert_eq!(
875 /// Ok(b"2 3 3 3?".to_vec()),
876 /// replace_all(&re, b"hi how are you?", &replacement),
877 /// );
878 /// assert!(replace_all(&re, b"hi there", &replacement).is_err());
879 /// ```
880 ///
881 /// # Example
882 ///
883 /// This example shows how to flip the order of whitespace (excluding line
884 /// terminators) delimited fields, and normalizes the whitespace that
885 /// delimits the fields:
886 ///
887 /// ```
888 /// use regex::bytes::Regex;
889 ///
890 /// let re = Regex::new(r"(?m)^(\S+)[\s--\r\n]+(\S+)$").unwrap();
891 /// let hay = b"
892 /// Greetings 1973
893 /// Wild\t1973
894 /// BornToRun\t\t\t\t1975
895 /// Darkness 1978
896 /// TheRiver 1980
897 /// ";
898 /// let new = re.replace_all(hay, b"$2 $1");
899 /// assert_eq!(new, &b"
900 /// 1973 Greetings
901 /// 1973 Wild
902 /// 1975 BornToRun
903 /// 1978 Darkness
904 /// 1980 TheRiver
905 /// "[..]);
906 /// ```
907 #[inline]
908 pub fn replace_all<'h, R: Replacer>(
909 &self,
910 haystack: &'h [u8],
911 rep: R,
912 ) -> Cow<'h, [u8]> {
913 self.replacen(haystack, 0, rep)
914 }
915
916 /// Replaces at most `limit` non-overlapping matches in the haystack with
917 /// the replacement provided. If `limit` is `0`, then all non-overlapping
918 /// matches are replaced. That is, `Regex::replace_all(hay, rep)` is
919 /// equivalent to `Regex::replacen(hay, 0, rep)`.
920 ///
921 /// If no match is found, then the haystack is returned unchanged. In that
922 /// case, this implementation will likely return a `Cow::Borrowed` value
923 /// such that no allocation is performed.
924 ///
925 /// When a `Cow::Borrowed` is returned, the value returned is guaranteed
926 /// to be equivalent to the `haystack` given.
927 ///
928 /// The documentation for [`Regex::replace`] goes into more detail about
929 /// what kinds of replacement strings are supported.
930 ///
931 /// # Time complexity
932 ///
933 /// Since iterators over all matches requires running potentially many
934 /// searches on the haystack, and since each search has worst case
935 /// `O(m * n)` time complexity, the overall worst case time complexity for
936 /// this routine is `O(m * n^2)`.
937 ///
938 /// Although note that the worst case time here has an upper bound given
939 /// by the `limit` parameter.
940 ///
941 /// # Fallibility
942 ///
943 /// See the corresponding section in the docs for [`Regex::replace_all`]
944 /// for tips on how to deal with a replacement routine that can fail.
945 ///
946 /// # Example
947 ///
948 /// This example shows how to flip the order of whitespace (excluding line
949 /// terminators) delimited fields, and normalizes the whitespace that
950 /// delimits the fields. But we only do it for the first two matches.
951 ///
952 /// ```
953 /// use regex::bytes::Regex;
954 ///
955 /// let re = Regex::new(r"(?m)^(\S+)[\s--\r\n]+(\S+)$").unwrap();
956 /// let hay = b"
957 /// Greetings 1973
958 /// Wild\t1973
959 /// BornToRun\t\t\t\t1975
960 /// Darkness 1978
961 /// TheRiver 1980
962 /// ";
963 /// let new = re.replacen(hay, 2, b"$2 $1");
964 /// assert_eq!(new, &b"
965 /// 1973 Greetings
966 /// 1973 Wild
967 /// BornToRun\t\t\t\t1975
968 /// Darkness 1978
969 /// TheRiver 1980
970 /// "[..]);
971 /// ```
972 #[inline]
973 pub fn replacen<'h, R: Replacer>(
974 &self,
975 haystack: &'h [u8],
976 limit: usize,
977 mut rep: R,
978 ) -> Cow<'h, [u8]> {
979 // If we know that the replacement doesn't have any capture expansions,
980 // then we can use the fast path. The fast path can make a tremendous
981 // difference:
982 //
983 // 1) We use `find_iter` instead of `captures_iter`. Not asking for
984 // captures generally makes the regex engines faster.
985 // 2) We don't need to look up all of the capture groups and do
986 // replacements inside the replacement string. We just push it
987 // at each match and be done with it.
988 if let Some(rep) = rep.no_expansion() {
989 let mut it = self.find_iter(haystack).enumerate().peekable();
990 if it.peek().is_none() {
991 return Cow::Borrowed(haystack);
992 }
993 let mut new = Vec::with_capacity(haystack.len());
994 let mut last_match = 0;
995 for (i, m) in it {
996 new.extend_from_slice(&haystack[last_match..m.start()]);
997 new.extend_from_slice(&rep);
998 last_match = m.end();
999 if limit > 0 && i >= limit - 1 {
1000 break;
1001 }
1002 }
1003 new.extend_from_slice(&haystack[last_match..]);
1004 return Cow::Owned(new);
1005 }
1006
1007 // The slower path, which we use if the replacement needs access to
1008 // capture groups.
1009 let mut it = self.captures_iter(haystack).enumerate().peekable();
1010 if it.peek().is_none() {
1011 return Cow::Borrowed(haystack);
1012 }
1013 let mut new = Vec::with_capacity(haystack.len());
1014 let mut last_match = 0;
1015 for (i, cap) in it {
1016 // unwrap on 0 is OK because captures only reports matches
1017 let m = cap.get(0).unwrap();
1018 new.extend_from_slice(&haystack[last_match..m.start()]);
1019 rep.replace_append(&cap, &mut new);
1020 last_match = m.end();
1021 if limit > 0 && i >= limit - 1 {
1022 break;
1023 }
1024 }
1025 new.extend_from_slice(&haystack[last_match..]);
1026 Cow::Owned(new)
1027 }
1028}
1029
1030/// A group of advanced or "lower level" search methods. Some methods permit
1031/// starting the search at a position greater than `0` in the haystack. Other
1032/// methods permit reusing allocations, for example, when extracting the
1033/// matches for capture groups.
1034impl Regex {
1035 /// Returns the end byte offset of the first match in the haystack given.
1036 ///
1037 /// This method may have the same performance characteristics as
1038 /// `is_match`. Behaviorally, it doesn't just report whether it match
1039 /// occurs, but also the end offset for a match. In particular, the offset
1040 /// returned *may be shorter* than the proper end of the leftmost-first
1041 /// match that you would find via [`Regex::find`].
1042 ///
1043 /// Note that it is not guaranteed that this routine finds the shortest or
1044 /// "earliest" possible match. Instead, the main idea of this API is that
1045 /// it returns the offset at the point at which the internal regex engine
1046 /// has determined that a match has occurred. This may vary depending on
1047 /// which internal regex engine is used, and thus, the offset itself may
1048 /// change based on internal heuristics.
1049 ///
1050 /// # Example
1051 ///
1052 /// Typically, `a+` would match the entire first sequence of `a` in some
1053 /// haystack, but `shortest_match` *may* give up as soon as it sees the
1054 /// first `a`.
1055 ///
1056 /// ```
1057 /// use regex::bytes::Regex;
1058 ///
1059 /// let re = Regex::new(r"a+").unwrap();
1060 /// let offset = re.shortest_match(b"aaaaa").unwrap();
1061 /// assert_eq!(offset, 1);
1062 /// ```
1063 #[inline]
1064 pub fn shortest_match(&self, haystack: &[u8]) -> Option<usize> {
1065 self.shortest_match_at(haystack, 0)
1066 }
1067
1068 /// Returns the same as `shortest_match`, but starts the search at the
1069 /// given offset.
1070 ///
1071 /// The significance of the starting point is that it takes the surrounding
1072 /// context into consideration. For example, the `\A` anchor can only match
1073 /// when `start == 0`.
1074 ///
1075 /// If a match is found, the offset returned is relative to the beginning
1076 /// of the haystack, not the beginning of the search.
1077 ///
1078 /// # Panics
1079 ///
1080 /// This panics when `start >= haystack.len() + 1`.
1081 ///
1082 /// # Example
1083 ///
1084 /// This example shows the significance of `start` by demonstrating how it
1085 /// can be used to permit look-around assertions in a regex to take the
1086 /// surrounding context into account.
1087 ///
1088 /// ```
1089 /// use regex::bytes::Regex;
1090 ///
1091 /// let re = Regex::new(r"\bchew\b").unwrap();
1092 /// let hay = b"eschew";
1093 /// // We get a match here, but it's probably not intended.
1094 /// assert_eq!(re.shortest_match(&hay[2..]), Some(4));
1095 /// // No match because the assertions take the context into account.
1096 /// assert_eq!(re.shortest_match_at(hay, 2), None);
1097 /// ```
1098 #[inline]
1099 pub fn shortest_match_at(
1100 &self,
1101 haystack: &[u8],
1102 start: usize,
1103 ) -> Option<usize> {
1104 let input =
1105 Input::new(haystack).earliest(true).span(start..haystack.len());
1106 self.meta.search_half(&input).map(|hm| hm.offset())
1107 }
1108
1109 /// Returns the same as [`Regex::is_match`], but starts the search at the
1110 /// given offset.
1111 ///
1112 /// The significance of the starting point is that it takes the surrounding
1113 /// context into consideration. For example, the `\A` anchor can only
1114 /// match when `start == 0`.
1115 ///
1116 /// # Panics
1117 ///
1118 /// This panics when `start >= haystack.len() + 1`.
1119 ///
1120 /// # Example
1121 ///
1122 /// This example shows the significance of `start` by demonstrating how it
1123 /// can be used to permit look-around assertions in a regex to take the
1124 /// surrounding context into account.
1125 ///
1126 /// ```
1127 /// use regex::bytes::Regex;
1128 ///
1129 /// let re = Regex::new(r"\bchew\b").unwrap();
1130 /// let hay = b"eschew";
1131 /// // We get a match here, but it's probably not intended.
1132 /// assert!(re.is_match(&hay[2..]));
1133 /// // No match because the assertions take the context into account.
1134 /// assert!(!re.is_match_at(hay, 2));
1135 /// ```
1136 #[inline]
1137 pub fn is_match_at(&self, haystack: &[u8], start: usize) -> bool {
1138 self.meta.is_match(Input::new(haystack).span(start..haystack.len()))
1139 }
1140
1141 /// Returns the same as [`Regex::find`], but starts the search at the given
1142 /// offset.
1143 ///
1144 /// The significance of the starting point is that it takes the surrounding
1145 /// context into consideration. For example, the `\A` anchor can only
1146 /// match when `start == 0`.
1147 ///
1148 /// # Panics
1149 ///
1150 /// This panics when `start >= haystack.len() + 1`.
1151 ///
1152 /// # Example
1153 ///
1154 /// This example shows the significance of `start` by demonstrating how it
1155 /// can be used to permit look-around assertions in a regex to take the
1156 /// surrounding context into account.
1157 ///
1158 /// ```
1159 /// use regex::bytes::Regex;
1160 ///
1161 /// let re = Regex::new(r"\bchew\b").unwrap();
1162 /// let hay = b"eschew";
1163 /// // We get a match here, but it's probably not intended.
1164 /// assert_eq!(re.find(&hay[2..]).map(|m| m.range()), Some(0..4));
1165 /// // No match because the assertions take the context into account.
1166 /// assert_eq!(re.find_at(hay, 2), None);
1167 /// ```
1168 #[inline]
1169 pub fn find_at<'h>(
1170 &self,
1171 haystack: &'h [u8],
1172 start: usize,
1173 ) -> Option<Match<'h>> {
1174 let input = Input::new(haystack).span(start..haystack.len());
1175 self.meta.find(input).map(|m| Match::new(haystack, m.start(), m.end()))
1176 }
1177
1178 /// Returns the same as [`Regex::captures`], but starts the search at the
1179 /// given offset.
1180 ///
1181 /// The significance of the starting point is that it takes the surrounding
1182 /// context into consideration. For example, the `\A` anchor can only
1183 /// match when `start == 0`.
1184 ///
1185 /// # Panics
1186 ///
1187 /// This panics when `start >= haystack.len() + 1`.
1188 ///
1189 /// # Example
1190 ///
1191 /// This example shows the significance of `start` by demonstrating how it
1192 /// can be used to permit look-around assertions in a regex to take the
1193 /// surrounding context into account.
1194 ///
1195 /// ```
1196 /// use regex::bytes::Regex;
1197 ///
1198 /// let re = Regex::new(r"\bchew\b").unwrap();
1199 /// let hay = b"eschew";
1200 /// // We get a match here, but it's probably not intended.
1201 /// assert_eq!(&re.captures(&hay[2..]).unwrap()[0], b"chew");
1202 /// // No match because the assertions take the context into account.
1203 /// assert!(re.captures_at(hay, 2).is_none());
1204 /// ```
1205 #[inline]
1206 pub fn captures_at<'h>(
1207 &self,
1208 haystack: &'h [u8],
1209 start: usize,
1210 ) -> Option<Captures<'h>> {
1211 let input = Input::new(haystack).span(start..haystack.len());
1212 let mut caps = self.meta.create_captures();
1213 self.meta.captures(input, &mut caps);
1214 if caps.is_match() {
1215 let static_captures_len = self.static_captures_len();
1216 Some(Captures { haystack, caps, static_captures_len })
1217 } else {
1218 None
1219 }
1220 }
1221
1222 /// This is like [`Regex::captures`], but writes the byte offsets of each
1223 /// capture group match into the locations given.
1224 ///
1225 /// A [`CaptureLocations`] stores the same byte offsets as a [`Captures`],
1226 /// but does *not* store a reference to the haystack. This makes its API
1227 /// a bit lower level and less convenient. But in exchange, callers
1228 /// may allocate their own `CaptureLocations` and reuse it for multiple
1229 /// searches. This may be helpful if allocating a `Captures` shows up in a
1230 /// profile as too costly.
1231 ///
1232 /// To create a `CaptureLocations` value, use the
1233 /// [`Regex::capture_locations`] method.
1234 ///
1235 /// This also returns the overall match if one was found. When a match is
1236 /// found, its offsets are also always stored in `locs` at index `0`.
1237 ///
1238 /// # Example
1239 ///
1240 /// ```
1241 /// use regex::bytes::Regex;
1242 ///
1243 /// let re = Regex::new(r"^([a-z]+)=(\S*)$").unwrap();
1244 /// let mut locs = re.capture_locations();
1245 /// assert!(re.captures_read(&mut locs, b"id=foo123").is_some());
1246 /// assert_eq!(Some((0, 9)), locs.get(0));
1247 /// assert_eq!(Some((0, 2)), locs.get(1));
1248 /// assert_eq!(Some((3, 9)), locs.get(2));
1249 /// ```
1250 #[inline]
1251 pub fn captures_read<'h>(
1252 &self,
1253 locs: &mut CaptureLocations,
1254 haystack: &'h [u8],
1255 ) -> Option<Match<'h>> {
1256 self.captures_read_at(locs, haystack, 0)
1257 }
1258
1259 /// Returns the same as [`Regex::captures_read`], but starts the search at
1260 /// the given offset.
1261 ///
1262 /// The significance of the starting point is that it takes the surrounding
1263 /// context into consideration. For example, the `\A` anchor can only
1264 /// match when `start == 0`.
1265 ///
1266 /// # Panics
1267 ///
1268 /// This panics when `start >= haystack.len() + 1`.
1269 ///
1270 /// # Example
1271 ///
1272 /// This example shows the significance of `start` by demonstrating how it
1273 /// can be used to permit look-around assertions in a regex to take the
1274 /// surrounding context into account.
1275 ///
1276 /// ```
1277 /// use regex::bytes::Regex;
1278 ///
1279 /// let re = Regex::new(r"\bchew\b").unwrap();
1280 /// let hay = b"eschew";
1281 /// let mut locs = re.capture_locations();
1282 /// // We get a match here, but it's probably not intended.
1283 /// assert!(re.captures_read(&mut locs, &hay[2..]).is_some());
1284 /// // No match because the assertions take the context into account.
1285 /// assert!(re.captures_read_at(&mut locs, hay, 2).is_none());
1286 /// ```
1287 #[inline]
1288 pub fn captures_read_at<'h>(
1289 &self,
1290 locs: &mut CaptureLocations,
1291 haystack: &'h [u8],
1292 start: usize,
1293 ) -> Option<Match<'h>> {
1294 let input = Input::new(haystack).span(start..haystack.len());
1295 self.meta.search_captures(&input, &mut locs.0);
1296 locs.0.get_match().map(|m| Match::new(haystack, m.start(), m.end()))
1297 }
1298
1299 /// An undocumented alias for `captures_read_at`.
1300 ///
1301 /// The `regex-capi` crate previously used this routine, so to avoid
1302 /// breaking that crate, we continue to provide the name as an undocumented
1303 /// alias.
1304 #[doc(hidden)]
1305 #[inline]
1306 pub fn read_captures_at<'h>(
1307 &self,
1308 locs: &mut CaptureLocations,
1309 haystack: &'h [u8],
1310 start: usize,
1311 ) -> Option<Match<'h>> {
1312 self.captures_read_at(locs, haystack, start)
1313 }
1314}
1315
1316/// Auxiliary methods.
1317impl Regex {
1318 /// Returns the original string of this regex.
1319 ///
1320 /// # Example
1321 ///
1322 /// ```
1323 /// use regex::bytes::Regex;
1324 ///
1325 /// let re = Regex::new(r"foo\w+bar").unwrap();
1326 /// assert_eq!(re.as_str(), r"foo\w+bar");
1327 /// ```
1328 #[inline]
1329 pub fn as_str(&self) -> &str {
1330 &self.pattern
1331 }
1332
1333 /// Returns an iterator over the capture names in this regex.
1334 ///
1335 /// The iterator returned yields elements of type `Option<&str>`. That is,
1336 /// the iterator yields values for all capture groups, even ones that are
1337 /// unnamed. The order of the groups corresponds to the order of the group's
1338 /// corresponding opening parenthesis.
1339 ///
1340 /// The first element of the iterator always yields the group corresponding
1341 /// to the overall match, and this group is always unnamed. Therefore, the
1342 /// iterator always yields at least one group.
1343 ///
1344 /// # Example
1345 ///
1346 /// This shows basic usage with a mix of named and unnamed capture groups:
1347 ///
1348 /// ```
1349 /// use regex::bytes::Regex;
1350 ///
1351 /// let re = Regex::new(r"(?<a>.(?<b>.))(.)(?:.)(?<c>.)").unwrap();
1352 /// let mut names = re.capture_names();
1353 /// assert_eq!(names.next(), Some(None));
1354 /// assert_eq!(names.next(), Some(Some("a")));
1355 /// assert_eq!(names.next(), Some(Some("b")));
1356 /// assert_eq!(names.next(), Some(None));
1357 /// // the '(?:.)' group is non-capturing and so doesn't appear here!
1358 /// assert_eq!(names.next(), Some(Some("c")));
1359 /// assert_eq!(names.next(), None);
1360 /// ```
1361 ///
1362 /// The iterator always yields at least one element, even for regexes with
1363 /// no capture groups and even for regexes that can never match:
1364 ///
1365 /// ```
1366 /// use regex::bytes::Regex;
1367 ///
1368 /// let re = Regex::new(r"").unwrap();
1369 /// let mut names = re.capture_names();
1370 /// assert_eq!(names.next(), Some(None));
1371 /// assert_eq!(names.next(), None);
1372 ///
1373 /// let re = Regex::new(r"[a&&b]").unwrap();
1374 /// let mut names = re.capture_names();
1375 /// assert_eq!(names.next(), Some(None));
1376 /// assert_eq!(names.next(), None);
1377 /// ```
1378 #[inline]
1379 pub fn capture_names(&self) -> CaptureNames<'_> {
1380 CaptureNames(self.meta.group_info().pattern_names(PatternID::ZERO))
1381 }
1382
1383 /// Returns the number of captures groups in this regex.
1384 ///
1385 /// This includes all named and unnamed groups, including the implicit
1386 /// unnamed group that is always present and corresponds to the entire
1387 /// match.
1388 ///
1389 /// Since the implicit unnamed group is always included in this length, the
1390 /// length returned is guaranteed to be greater than zero.
1391 ///
1392 /// # Example
1393 ///
1394 /// ```
1395 /// use regex::bytes::Regex;
1396 ///
1397 /// let re = Regex::new(r"foo").unwrap();
1398 /// assert_eq!(1, re.captures_len());
1399 ///
1400 /// let re = Regex::new(r"(foo)").unwrap();
1401 /// assert_eq!(2, re.captures_len());
1402 ///
1403 /// let re = Regex::new(r"(?<a>.(?<b>.))(.)(?:.)(?<c>.)").unwrap();
1404 /// assert_eq!(5, re.captures_len());
1405 ///
1406 /// let re = Regex::new(r"[a&&b]").unwrap();
1407 /// assert_eq!(1, re.captures_len());
1408 /// ```
1409 #[inline]
1410 pub fn captures_len(&self) -> usize {
1411 self.meta.group_info().group_len(PatternID::ZERO)
1412 }
1413
1414 /// Returns the total number of capturing groups that appear in every
1415 /// possible match.
1416 ///
1417 /// If the number of capture groups can vary depending on the match, then
1418 /// this returns `None`. That is, a value is only returned when the number
1419 /// of matching groups is invariant or "static."
1420 ///
1421 /// Note that like [`Regex::captures_len`], this **does** include the
1422 /// implicit capturing group corresponding to the entire match. Therefore,
1423 /// when a non-None value is returned, it is guaranteed to be at least `1`.
1424 /// Stated differently, a return value of `Some(0)` is impossible.
1425 ///
1426 /// # Example
1427 ///
1428 /// This shows a few cases where a static number of capture groups is
1429 /// available and a few cases where it is not.
1430 ///
1431 /// ```
1432 /// use regex::bytes::Regex;
1433 ///
1434 /// let len = |pattern| {
1435 /// Regex::new(pattern).map(|re| re.static_captures_len())
1436 /// };
1437 ///
1438 /// assert_eq!(Some(1), len("a")?);
1439 /// assert_eq!(Some(2), len("(a)")?);
1440 /// assert_eq!(Some(2), len("(a)|(b)")?);
1441 /// assert_eq!(Some(3), len("(a)(b)|(c)(d)")?);
1442 /// assert_eq!(None, len("(a)|b")?);
1443 /// assert_eq!(None, len("a|(b)")?);
1444 /// assert_eq!(None, len("(b)*")?);
1445 /// assert_eq!(Some(2), len("(b)+")?);
1446 ///
1447 /// # Ok::<(), Box<dyn std::error::Error>>(())
1448 /// ```
1449 #[inline]
1450 pub fn static_captures_len(&self) -> Option<usize> {
1451 self.meta.static_captures_len()
1452 }
1453
1454 /// Returns a fresh allocated set of capture locations that can
1455 /// be reused in multiple calls to [`Regex::captures_read`] or
1456 /// [`Regex::captures_read_at`].
1457 ///
1458 /// # Example
1459 ///
1460 /// ```
1461 /// use regex::bytes::Regex;
1462 ///
1463 /// let re = Regex::new(r"(.)(.)(\w+)").unwrap();
1464 /// let mut locs = re.capture_locations();
1465 /// assert!(re.captures_read(&mut locs, b"Padron").is_some());
1466 /// assert_eq!(locs.get(0), Some((0, 6)));
1467 /// assert_eq!(locs.get(1), Some((0, 1)));
1468 /// assert_eq!(locs.get(2), Some((1, 2)));
1469 /// assert_eq!(locs.get(3), Some((2, 6)));
1470 /// ```
1471 #[inline]
1472 pub fn capture_locations(&self) -> CaptureLocations {
1473 CaptureLocations(self.meta.create_captures())
1474 }
1475
1476 /// An alias for `capture_locations` to preserve backward compatibility.
1477 ///
1478 /// The `regex-capi` crate uses this method, so to avoid breaking that
1479 /// crate, we continue to export it as an undocumented API.
1480 #[doc(hidden)]
1481 #[inline]
1482 pub fn locations(&self) -> CaptureLocations {
1483 self.capture_locations()
1484 }
1485}
1486
1487/// Represents a single match of a regex in a haystack.
1488///
1489/// A `Match` contains both the start and end byte offsets of the match and the
1490/// actual substring corresponding to the range of those byte offsets. It is
1491/// guaranteed that `start <= end`. When `start == end`, the match is empty.
1492///
1493/// Unlike the top-level `Match` type, this `Match` type is produced by APIs
1494/// that search `&[u8]` haystacks. This means that the offsets in a `Match` can
1495/// point to anywhere in the haystack, including in a place that splits the
1496/// UTF-8 encoding of a Unicode scalar value.
1497///
1498/// The lifetime parameter `'h` refers to the lifetime of the matched of the
1499/// haystack that this match was produced from.
1500///
1501/// # Numbering
1502///
1503/// The byte offsets in a `Match` form a half-open interval. That is, the
1504/// start of the range is inclusive and the end of the range is exclusive.
1505/// For example, given a haystack `abcFOOxyz` and a match of `FOO`, its byte
1506/// offset range starts at `3` and ends at `6`. `3` corresponds to `F` and
1507/// `6` corresponds to `x`, which is one past the end of the match. This
1508/// corresponds to the same kind of slicing that Rust uses.
1509///
1510/// For more on why this was chosen over other schemes (aside from being
1511/// consistent with how Rust the language works), see [this discussion] and
1512/// [Dijkstra's note on a related topic][note].
1513///
1514/// [this discussion]: https://github.com/rust-lang/regex/discussions/866
1515/// [note]: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
1516///
1517/// # Example
1518///
1519/// This example shows the value of each of the methods on `Match` for a
1520/// particular search.
1521///
1522/// ```
1523/// use regex::bytes::Regex;
1524///
1525/// let re = Regex::new(r"\p{Greek}+").unwrap();
1526/// let hay = "Greek: αβγδ".as_bytes();
1527/// let m = re.find(hay).unwrap();
1528/// assert_eq!(7, m.start());
1529/// assert_eq!(15, m.end());
1530/// assert!(!m.is_empty());
1531/// assert_eq!(8, m.len());
1532/// assert_eq!(7..15, m.range());
1533/// assert_eq!("αβγδ".as_bytes(), m.as_bytes());
1534/// ```
1535#[derive(Copy, Clone, Eq, PartialEq)]
1536pub struct Match<'h> {
1537 haystack: &'h [u8],
1538 start: usize,
1539 end: usize,
1540}
1541
1542impl<'h> Match<'h> {
1543 /// Returns the byte offset of the start of the match in the haystack. The
1544 /// start of the match corresponds to the position where the match begins
1545 /// and includes the first byte in the match.
1546 ///
1547 /// It is guaranteed that `Match::start() <= Match::end()`.
1548 ///
1549 /// Unlike the top-level `Match` type, the start offset may appear anywhere
1550 /// in the haystack. This includes between the code units of a UTF-8
1551 /// encoded Unicode scalar value.
1552 #[inline]
1553 pub fn start(&self) -> usize {
1554 self.start
1555 }
1556
1557 /// Returns the byte offset of the end of the match in the haystack. The
1558 /// end of the match corresponds to the byte immediately following the last
1559 /// byte in the match. This means that `&slice[start..end]` works as one
1560 /// would expect.
1561 ///
1562 /// It is guaranteed that `Match::start() <= Match::end()`.
1563 ///
1564 /// Unlike the top-level `Match` type, the start offset may appear anywhere
1565 /// in the haystack. This includes between the code units of a UTF-8
1566 /// encoded Unicode scalar value.
1567 #[inline]
1568 pub fn end(&self) -> usize {
1569 self.end
1570 }
1571
1572 /// Returns true if and only if this match has a length of zero.
1573 ///
1574 /// Note that an empty match can only occur when the regex itself can
1575 /// match the empty string. Here are some examples of regexes that can
1576 /// all match the empty string: `^`, `^$`, `\b`, `a?`, `a*`, `a{0}`,
1577 /// `(foo|\d+|quux)?`.
1578 #[inline]
1579 pub fn is_empty(&self) -> bool {
1580 self.start == self.end
1581 }
1582
1583 /// Returns the length, in bytes, of this match.
1584 #[inline]
1585 pub fn len(&self) -> usize {
1586 self.end - self.start
1587 }
1588
1589 /// Returns the range over the starting and ending byte offsets of the
1590 /// match in the haystack.
1591 #[inline]
1592 pub fn range(&self) -> core::ops::Range<usize> {
1593 self.start..self.end
1594 }
1595
1596 /// Returns the substring of the haystack that matched.
1597 #[inline]
1598 pub fn as_bytes(&self) -> &'h [u8] {
1599 &self.haystack[self.range()]
1600 }
1601
1602 /// Creates a new match from the given haystack and byte offsets.
1603 #[inline]
1604 fn new(haystack: &'h [u8], start: usize, end: usize) -> Match<'h> {
1605 Match { haystack, start, end }
1606 }
1607}
1608
1609impl<'h> core::fmt::Debug for Match<'h> {
1610 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1611 use regex_automata::util::escape::DebugHaystack;
1612
1613 let mut fmt = f.debug_struct("Match");
1614 fmt.field("start", &self.start)
1615 .field("end", &self.end)
1616 .field("bytes", &DebugHaystack(&self.as_bytes()));
1617
1618 fmt.finish()
1619 }
1620}
1621
1622impl<'h> From<Match<'h>> for &'h [u8] {
1623 fn from(m: Match<'h>) -> &'h [u8] {
1624 m.as_bytes()
1625 }
1626}
1627
1628impl<'h> From<Match<'h>> for core::ops::Range<usize> {
1629 fn from(m: Match<'h>) -> core::ops::Range<usize> {
1630 m.range()
1631 }
1632}
1633
1634/// Represents the capture groups for a single match.
1635///
1636/// Capture groups refer to parts of a regex enclosed in parentheses. They
1637/// can be optionally named. The purpose of capture groups is to be able to
1638/// reference different parts of a match based on the original pattern. In
1639/// essence, a `Captures` is a container of [`Match`] values for each group
1640/// that participated in a regex match. Each `Match` can be looked up by either
1641/// its capture group index or name (if it has one).
1642///
1643/// For example, say you want to match the individual letters in a 5-letter
1644/// word:
1645///
1646/// ```text
1647/// (?<first>\w)(\w)(?:\w)\w(?<last>\w)
1648/// ```
1649///
1650/// This regex has 4 capture groups:
1651///
1652/// * The group at index `0` corresponds to the overall match. It is always
1653/// present in every match and never has a name.
1654/// * The group at index `1` with name `first` corresponding to the first
1655/// letter.
1656/// * The group at index `2` with no name corresponding to the second letter.
1657/// * The group at index `3` with name `last` corresponding to the fifth and
1658/// last letter.
1659///
1660/// Notice that `(?:\w)` was not listed above as a capture group despite it
1661/// being enclosed in parentheses. That's because `(?:pattern)` is a special
1662/// syntax that permits grouping but *without* capturing. The reason for not
1663/// treating it as a capture is that tracking and reporting capture groups
1664/// requires additional state that may lead to slower searches. So using as few
1665/// capture groups as possible can help performance. (Although the difference
1666/// in performance of a couple of capture groups is likely immaterial.)
1667///
1668/// Values with this type are created by [`Regex::captures`] or
1669/// [`Regex::captures_iter`].
1670///
1671/// `'h` is the lifetime of the haystack that these captures were matched from.
1672///
1673/// # Example
1674///
1675/// ```
1676/// use regex::bytes::Regex;
1677///
1678/// let re = Regex::new(r"(?<first>\w)(\w)(?:\w)\w(?<last>\w)").unwrap();
1679/// let caps = re.captures(b"toady").unwrap();
1680/// assert_eq!(b"toady", &caps[0]);
1681/// assert_eq!(b"t", &caps["first"]);
1682/// assert_eq!(b"o", &caps[2]);
1683/// assert_eq!(b"y", &caps["last"]);
1684/// ```
1685pub struct Captures<'h> {
1686 haystack: &'h [u8],
1687 caps: captures::Captures,
1688 static_captures_len: Option<usize>,
1689}
1690
1691impl<'h> Captures<'h> {
1692 /// Returns the `Match` associated with the capture group at index `i`. If
1693 /// `i` does not correspond to a capture group, or if the capture group did
1694 /// not participate in the match, then `None` is returned.
1695 ///
1696 /// When `i == 0`, this is guaranteed to return a non-`None` value.
1697 ///
1698 /// # Examples
1699 ///
1700 /// Get the substring that matched with a default of an empty string if the
1701 /// group didn't participate in the match:
1702 ///
1703 /// ```
1704 /// use regex::bytes::Regex;
1705 ///
1706 /// let re = Regex::new(r"[a-z]+(?:([0-9]+)|([A-Z]+))").unwrap();
1707 /// let caps = re.captures(b"abc123").unwrap();
1708 ///
1709 /// let substr1 = caps.get(1).map_or(&b""[..], |m| m.as_bytes());
1710 /// let substr2 = caps.get(2).map_or(&b""[..], |m| m.as_bytes());
1711 /// assert_eq!(substr1, b"123");
1712 /// assert_eq!(substr2, b"");
1713 /// ```
1714 #[inline]
1715 pub fn get(&self, i: usize) -> Option<Match<'h>> {
1716 self.caps
1717 .get_group(i)
1718 .map(|sp| Match::new(self.haystack, sp.start, sp.end))
1719 }
1720
1721 /// Return the overall match for the capture.
1722 ///
1723 /// This returns the match for index `0`. That is it is equivalent to
1724 /// `m.get(0).unwrap()`
1725 ///
1726 /// # Example
1727 ///
1728 /// ```
1729 /// use regex::bytes::Regex;
1730 ///
1731 /// let re = Regex::new(r"[a-z]+([0-9]+)").unwrap();
1732 /// let caps = re.captures(b" abc123-def").unwrap();
1733 ///
1734 /// assert_eq!(caps.get_match().as_bytes(), b"abc123");
1735 /// ```
1736 #[inline]
1737 pub fn get_match(&self) -> Match<'h> {
1738 self.get(0).unwrap()
1739 }
1740
1741 /// Returns the `Match` associated with the capture group named `name`. If
1742 /// `name` isn't a valid capture group or it refers to a group that didn't
1743 /// match, then `None` is returned.
1744 ///
1745 /// Note that unlike `caps["name"]`, this returns a `Match` whose lifetime
1746 /// matches the lifetime of the haystack in this `Captures` value.
1747 /// Conversely, the substring returned by `caps["name"]` has a lifetime
1748 /// of the `Captures` value, which is likely shorter than the lifetime of
1749 /// the haystack. In some cases, it may be necessary to use this method to
1750 /// access the matching substring instead of the `caps["name"]` notation.
1751 ///
1752 /// # Examples
1753 ///
1754 /// Get the substring that matched with a default of an empty string if the
1755 /// group didn't participate in the match:
1756 ///
1757 /// ```
1758 /// use regex::bytes::Regex;
1759 ///
1760 /// let re = Regex::new(
1761 /// r"[a-z]+(?:(?<numbers>[0-9]+)|(?<letters>[A-Z]+))",
1762 /// ).unwrap();
1763 /// let caps = re.captures(b"abc123").unwrap();
1764 ///
1765 /// let numbers = caps.name("numbers").map_or(&b""[..], |m| m.as_bytes());
1766 /// let letters = caps.name("letters").map_or(&b""[..], |m| m.as_bytes());
1767 /// assert_eq!(numbers, b"123");
1768 /// assert_eq!(letters, b"");
1769 /// ```
1770 #[inline]
1771 pub fn name(&self, name: &str) -> Option<Match<'h>> {
1772 self.caps
1773 .get_group_by_name(name)
1774 .map(|sp| Match::new(self.haystack, sp.start, sp.end))
1775 }
1776
1777 /// This is a convenience routine for extracting the substrings
1778 /// corresponding to matching capture groups.
1779 ///
1780 /// This returns a tuple where the first element corresponds to the full
1781 /// substring of the haystack that matched the regex. The second element is
1782 /// an array of substrings, with each corresponding to the substring that
1783 /// matched for a particular capture group.
1784 ///
1785 /// # Panics
1786 ///
1787 /// This panics if the number of possible matching groups in this
1788 /// `Captures` value is not fixed to `N` in all circumstances.
1789 /// More precisely, this routine only works when `N` is equivalent to
1790 /// [`Regex::static_captures_len`].
1791 ///
1792 /// Stated more plainly, if the number of matching capture groups in a
1793 /// regex can vary from match to match, then this function always panics.
1794 ///
1795 /// For example, `(a)(b)|(c)` could produce two matching capture groups
1796 /// or one matching capture group for any given match. Therefore, one
1797 /// cannot use `extract` with such a pattern.
1798 ///
1799 /// But a pattern like `(a)(b)|(c)(d)` can be used with `extract` because
1800 /// the number of capture groups in every match is always equivalent,
1801 /// even if the capture _indices_ in each match are not.
1802 ///
1803 /// # Example
1804 ///
1805 /// ```
1806 /// use regex::bytes::Regex;
1807 ///
1808 /// let re = Regex::new(r"([0-9]{4})-([0-9]{2})-([0-9]{2})").unwrap();
1809 /// let hay = b"On 2010-03-14, I became a Tennessee lamb.";
1810 /// let Some((full, [year, month, day])) =
1811 /// re.captures(hay).map(|caps| caps.extract()) else { return };
1812 /// assert_eq!(b"2010-03-14", full);
1813 /// assert_eq!(b"2010", year);
1814 /// assert_eq!(b"03", month);
1815 /// assert_eq!(b"14", day);
1816 /// ```
1817 ///
1818 /// # Example: iteration
1819 ///
1820 /// This example shows how to use this method when iterating over all
1821 /// `Captures` matches in a haystack.
1822 ///
1823 /// ```
1824 /// use regex::bytes::Regex;
1825 ///
1826 /// let re = Regex::new(r"([0-9]{4})-([0-9]{2})-([0-9]{2})").unwrap();
1827 /// let hay = b"1973-01-05, 1975-08-25 and 1980-10-18";
1828 ///
1829 /// let mut dates: Vec<(&[u8], &[u8], &[u8])> = vec![];
1830 /// for (_, [y, m, d]) in re.captures_iter(hay).map(|c| c.extract()) {
1831 /// dates.push((y, m, d));
1832 /// }
1833 /// assert_eq!(dates, vec![
1834 /// (&b"1973"[..], &b"01"[..], &b"05"[..]),
1835 /// (&b"1975"[..], &b"08"[..], &b"25"[..]),
1836 /// (&b"1980"[..], &b"10"[..], &b"18"[..]),
1837 /// ]);
1838 /// ```
1839 ///
1840 /// # Example: parsing different formats
1841 ///
1842 /// This API is particularly useful when you need to extract a particular
1843 /// value that might occur in a different format. Consider, for example,
1844 /// an identifier that might be in double quotes or single quotes:
1845 ///
1846 /// ```
1847 /// use regex::bytes::Regex;
1848 ///
1849 /// let re = Regex::new(r#"id:(?:"([^"]+)"|'([^']+)')"#).unwrap();
1850 /// let hay = br#"The first is id:"foo" and the second is id:'bar'."#;
1851 /// let mut ids = vec![];
1852 /// for (_, [id]) in re.captures_iter(hay).map(|c| c.extract()) {
1853 /// ids.push(id);
1854 /// }
1855 /// assert_eq!(ids, vec![b"foo", b"bar"]);
1856 /// ```
1857 pub fn extract<const N: usize>(&self) -> (&'h [u8], [&'h [u8]; N]) {
1858 let len = self
1859 .static_captures_len
1860 .expect("number of capture groups can vary in a match")
1861 .checked_sub(1)
1862 .expect("number of groups is always greater than zero");
1863 assert_eq!(N, len, "asked for {N} groups, but must ask for {len}");
1864 // The regex-automata variant of extract is a bit more permissive.
1865 // It doesn't require the number of matching capturing groups to be
1866 // static, and you can even request fewer groups than what's there. So
1867 // this is guaranteed to never panic because we've asserted above that
1868 // the user has requested precisely the number of groups that must be
1869 // present in any match for this regex.
1870 self.caps.extract_bytes(self.haystack)
1871 }
1872
1873 /// Expands all instances of `$ref` in `replacement` to the corresponding
1874 /// capture group, and writes them to the `dst` buffer given. A `ref` can
1875 /// be a capture group index or a name. If `ref` doesn't refer to a capture
1876 /// group that participated in the match, then it is replaced with the
1877 /// empty string.
1878 ///
1879 /// # Format
1880 ///
1881 /// The format of the replacement string supports two different kinds of
1882 /// capture references: unbraced and braced.
1883 ///
1884 /// For the unbraced format, the format supported is `$ref` where `name`
1885 /// can be any character in the class `[0-9A-Za-z_]`. `ref` is always
1886 /// the longest possible parse. So for example, `$1a` corresponds to the
1887 /// capture group named `1a` and not the capture group at index `1`. If
1888 /// `ref` matches `^[0-9]+$`, then it is treated as a capture group index
1889 /// itself and not a name.
1890 ///
1891 /// For the braced format, the format supported is `${ref}` where `ref` can
1892 /// be any sequence of bytes except for `}`. If no closing brace occurs,
1893 /// then it is not considered a capture reference. As with the unbraced
1894 /// format, if `ref` matches `^[0-9]+$`, then it is treated as a capture
1895 /// group index and not a name.
1896 ///
1897 /// The braced format is useful for exerting precise control over the name
1898 /// of the capture reference. For example, `${1}a` corresponds to the
1899 /// capture group reference `1` followed by the letter `a`, where as `$1a`
1900 /// (as mentioned above) corresponds to the capture group reference `1a`.
1901 /// The braced format is also useful for expressing capture group names
1902 /// that use characters not supported by the unbraced format. For example,
1903 /// `${foo[bar].baz}` refers to the capture group named `foo[bar].baz`.
1904 ///
1905 /// If a capture group reference is found and it does not refer to a valid
1906 /// capture group, then it will be replaced with the empty string.
1907 ///
1908 /// To write a literal `$`, use `$$`.
1909 ///
1910 /// # Example
1911 ///
1912 /// ```
1913 /// use regex::bytes::Regex;
1914 ///
1915 /// let re = Regex::new(
1916 /// r"(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[0-9]{4})",
1917 /// ).unwrap();
1918 /// let hay = b"On 14-03-2010, I became a Tennessee lamb.";
1919 /// let caps = re.captures(hay).unwrap();
1920 ///
1921 /// let mut dst = vec![];
1922 /// caps.expand(b"year=$year, month=$month, day=$day", &mut dst);
1923 /// assert_eq!(dst, b"year=2010, month=03, day=14");
1924 /// ```
1925 #[inline]
1926 pub fn expand(&self, replacement: &[u8], dst: &mut Vec<u8>) {
1927 self.caps.interpolate_bytes_into(self.haystack, replacement, dst);
1928 }
1929
1930 /// Returns an iterator over all capture groups. This includes both
1931 /// matching and non-matching groups.
1932 ///
1933 /// The iterator always yields at least one matching group: the first group
1934 /// (at index `0`) with no name. Subsequent groups are returned in the order
1935 /// of their opening parenthesis in the regex.
1936 ///
1937 /// The elements yielded have type `Option<Match<'h>>`, where a non-`None`
1938 /// value is present if the capture group matches.
1939 ///
1940 /// # Example
1941 ///
1942 /// ```
1943 /// use regex::bytes::Regex;
1944 ///
1945 /// let re = Regex::new(r"(\w)(\d)?(\w)").unwrap();
1946 /// let caps = re.captures(b"AZ").unwrap();
1947 ///
1948 /// let mut it = caps.iter();
1949 /// assert_eq!(it.next().unwrap().map(|m| m.as_bytes()), Some(&b"AZ"[..]));
1950 /// assert_eq!(it.next().unwrap().map(|m| m.as_bytes()), Some(&b"A"[..]));
1951 /// assert_eq!(it.next().unwrap().map(|m| m.as_bytes()), None);
1952 /// assert_eq!(it.next().unwrap().map(|m| m.as_bytes()), Some(&b"Z"[..]));
1953 /// assert_eq!(it.next(), None);
1954 /// ```
1955 #[inline]
1956 pub fn iter<'c>(&'c self) -> SubCaptureMatches<'c, 'h> {
1957 SubCaptureMatches { haystack: self.haystack, it: self.caps.iter() }
1958 }
1959
1960 /// Returns the total number of capture groups. This includes both
1961 /// matching and non-matching groups.
1962 ///
1963 /// The length returned is always equivalent to the number of elements
1964 /// yielded by [`Captures::iter`]. Consequently, the length is always
1965 /// greater than zero since every `Captures` value always includes the
1966 /// match for the entire regex.
1967 ///
1968 /// # Example
1969 ///
1970 /// ```
1971 /// use regex::bytes::Regex;
1972 ///
1973 /// let re = Regex::new(r"(\w)(\d)?(\w)").unwrap();
1974 /// let caps = re.captures(b"AZ").unwrap();
1975 /// assert_eq!(caps.len(), 4);
1976 /// ```
1977 #[inline]
1978 pub fn len(&self) -> usize {
1979 self.caps.group_len()
1980 }
1981}
1982
1983impl<'h> core::fmt::Debug for Captures<'h> {
1984 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1985 /// A little helper type to provide a nice map-like debug
1986 /// representation for our capturing group spans.
1987 ///
1988 /// regex-automata has something similar, but it includes the pattern
1989 /// ID in its debug output, which is confusing. It also doesn't include
1990 /// that strings that match because a regex-automata `Captures` doesn't
1991 /// borrow the haystack.
1992 struct CapturesDebugMap<'a> {
1993 caps: &'a Captures<'a>,
1994 }
1995
1996 impl<'a> core::fmt::Debug for CapturesDebugMap<'a> {
1997 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1998 let mut map = f.debug_map();
1999 let names =
2000 self.caps.caps.group_info().pattern_names(PatternID::ZERO);
2001 for (group_index, maybe_name) in names.enumerate() {
2002 let key = Key(group_index, maybe_name);
2003 match self.caps.get(group_index) {
2004 None => map.entry(&key, &None::<()>),
2005 Some(mat) => map.entry(&key, &Value(mat)),
2006 };
2007 }
2008 map.finish()
2009 }
2010 }
2011
2012 struct Key<'a>(usize, Option<&'a str>);
2013
2014 impl<'a> core::fmt::Debug for Key<'a> {
2015 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2016 write!(f, "{}", self.0)?;
2017 if let Some(name) = self.1 {
2018 write!(f, "/{name:?}")?;
2019 }
2020 Ok(())
2021 }
2022 }
2023
2024 struct Value<'a>(Match<'a>);
2025
2026 impl<'a> core::fmt::Debug for Value<'a> {
2027 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2028 use regex_automata::util::escape::DebugHaystack;
2029
2030 write!(
2031 f,
2032 "{}..{}/{:?}",
2033 self.0.start(),
2034 self.0.end(),
2035 DebugHaystack(self.0.as_bytes())
2036 )
2037 }
2038 }
2039
2040 f.debug_tuple("Captures")
2041 .field(&CapturesDebugMap { caps: self })
2042 .finish()
2043 }
2044}
2045
2046/// Get a matching capture group's haystack substring by index.
2047///
2048/// The haystack substring returned can't outlive the `Captures` object if this
2049/// method is used, because of how `Index` is defined (normally `a[i]` is part
2050/// of `a` and can't outlive it). To work around this limitation, do that, use
2051/// [`Captures::get`] instead.
2052///
2053/// `'h` is the lifetime of the matched haystack, but the lifetime of the
2054/// `&str` returned by this implementation is the lifetime of the `Captures`
2055/// value itself.
2056///
2057/// # Panics
2058///
2059/// If there is no matching group at the given index.
2060impl<'h> core::ops::Index<usize> for Captures<'h> {
2061 type Output = [u8];
2062
2063 // The lifetime is written out to make it clear that the &str returned
2064 // does NOT have a lifetime equivalent to 'h.
2065 fn index<'a>(&'a self, i: usize) -> &'a [u8] {
2066 self.get(i)
2067 .map(|m| m.as_bytes())
2068 .unwrap_or_else(|| panic!("no group at index '{i}'"))
2069 }
2070}
2071
2072/// Get a matching capture group's haystack substring by name.
2073///
2074/// The haystack substring returned can't outlive the `Captures` object if this
2075/// method is used, because of how `Index` is defined (normally `a[i]` is part
2076/// of `a` and can't outlive it). To work around this limitation, do that, use
2077/// [`Captures::name`] instead.
2078///
2079/// `'h` is the lifetime of the matched haystack, but the lifetime of the
2080/// `&str` returned by this implementation is the lifetime of the `Captures`
2081/// value itself.
2082///
2083/// `'n` is the lifetime of the group name used to index the `Captures` value.
2084///
2085/// # Panics
2086///
2087/// If there is no matching group at the given name.
2088impl<'h, 'n> core::ops::Index<&'n str> for Captures<'h> {
2089 type Output = [u8];
2090
2091 fn index<'a>(&'a self, name: &'n str) -> &'a [u8] {
2092 self.name(name)
2093 .map(|m| m.as_bytes())
2094 .unwrap_or_else(|| panic!("no group named '{name}'"))
2095 }
2096}
2097
2098/// A low level representation of the byte offsets of each capture group.
2099///
2100/// You can think of this as a lower level [`Captures`], where this type does
2101/// not support named capturing groups directly and it does not borrow the
2102/// haystack that these offsets were matched on.
2103///
2104/// Primarily, this type is useful when using the lower level `Regex` APIs such
2105/// as [`Regex::captures_read`], which permits amortizing the allocation in
2106/// which capture match offsets are stored.
2107///
2108/// In order to build a value of this type, you'll need to call the
2109/// [`Regex::capture_locations`] method. The value returned can then be reused
2110/// in subsequent searches for that regex. Using it for other regexes may
2111/// result in a panic or otherwise incorrect results.
2112///
2113/// # Example
2114///
2115/// This example shows how to create and use `CaptureLocations` in a search.
2116///
2117/// ```
2118/// use regex::bytes::Regex;
2119///
2120/// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
2121/// let mut locs = re.capture_locations();
2122/// let m = re.captures_read(&mut locs, b"Bruce Springsteen").unwrap();
2123/// assert_eq!(0..17, m.range());
2124/// assert_eq!(Some((0, 17)), locs.get(0));
2125/// assert_eq!(Some((0, 5)), locs.get(1));
2126/// assert_eq!(Some((6, 17)), locs.get(2));
2127///
2128/// // Asking for an invalid capture group always returns None.
2129/// assert_eq!(None, locs.get(3));
2130/// # // literals are too big for 32-bit usize: #1041
2131/// # #[cfg(target_pointer_width = "64")]
2132/// assert_eq!(None, locs.get(34973498648));
2133/// # #[cfg(target_pointer_width = "64")]
2134/// assert_eq!(None, locs.get(9944060567225171988));
2135/// ```
2136#[derive(Clone, Debug)]
2137pub struct CaptureLocations(captures::Captures);
2138
2139/// A type alias for `CaptureLocations` for backwards compatibility.
2140///
2141/// Previously, we exported `CaptureLocations` as `Locations` in an
2142/// undocumented API. To prevent breaking that code (e.g., in `regex-capi`),
2143/// we continue re-exporting the same undocumented API.
2144#[doc(hidden)]
2145pub type Locations = CaptureLocations;
2146
2147impl CaptureLocations {
2148 /// Returns the start and end byte offsets of the capture group at index
2149 /// `i`. This returns `None` if `i` is not a valid capture group or if the
2150 /// capture group did not match.
2151 ///
2152 /// # Example
2153 ///
2154 /// ```
2155 /// use regex::bytes::Regex;
2156 ///
2157 /// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
2158 /// let mut locs = re.capture_locations();
2159 /// re.captures_read(&mut locs, b"Bruce Springsteen").unwrap();
2160 /// assert_eq!(Some((0, 17)), locs.get(0));
2161 /// assert_eq!(Some((0, 5)), locs.get(1));
2162 /// assert_eq!(Some((6, 17)), locs.get(2));
2163 /// ```
2164 #[inline]
2165 pub fn get(&self, i: usize) -> Option<(usize, usize)> {
2166 self.0.get_group(i).map(|sp| (sp.start, sp.end))
2167 }
2168
2169 /// Returns the total number of capture groups (even if they didn't match).
2170 /// That is, the length returned is unaffected by the result of a search.
2171 ///
2172 /// This is always at least `1` since every regex has at least `1`
2173 /// capturing group that corresponds to the entire match.
2174 ///
2175 /// # Example
2176 ///
2177 /// ```
2178 /// use regex::bytes::Regex;
2179 ///
2180 /// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
2181 /// let mut locs = re.capture_locations();
2182 /// assert_eq!(3, locs.len());
2183 /// re.captures_read(&mut locs, b"Bruce Springsteen").unwrap();
2184 /// assert_eq!(3, locs.len());
2185 /// ```
2186 ///
2187 /// Notice that the length is always at least `1`, regardless of the regex:
2188 ///
2189 /// ```
2190 /// use regex::bytes::Regex;
2191 ///
2192 /// let re = Regex::new(r"").unwrap();
2193 /// let locs = re.capture_locations();
2194 /// assert_eq!(1, locs.len());
2195 ///
2196 /// // [a&&b] is a regex that never matches anything.
2197 /// let re = Regex::new(r"[a&&b]").unwrap();
2198 /// let locs = re.capture_locations();
2199 /// assert_eq!(1, locs.len());
2200 /// ```
2201 #[inline]
2202 pub fn len(&self) -> usize {
2203 // self.0.group_len() returns 0 if the underlying captures doesn't
2204 // represent a match, but the behavior guaranteed for this method is
2205 // that the length doesn't change based on a match or not.
2206 self.0.group_info().group_len(PatternID::ZERO)
2207 }
2208
2209 /// An alias for the `get` method for backwards compatibility.
2210 ///
2211 /// Previously, we exported `get` as `pos` in an undocumented API. To
2212 /// prevent breaking that code (e.g., in `regex-capi`), we continue
2213 /// re-exporting the same undocumented API.
2214 #[doc(hidden)]
2215 #[inline]
2216 pub fn pos(&self, i: usize) -> Option<(usize, usize)> {
2217 self.get(i)
2218 }
2219}
2220
2221/// An iterator over all non-overlapping matches in a haystack.
2222///
2223/// This iterator yields [`Match`] values. The iterator stops when no more
2224/// matches can be found.
2225///
2226/// `'r` is the lifetime of the compiled regular expression and `'h` is the
2227/// lifetime of the haystack.
2228///
2229/// This iterator is created by [`Regex::find_iter`].
2230///
2231/// # Time complexity
2232///
2233/// Note that since an iterator runs potentially many searches on the haystack
2234/// and since each search has worst case `O(m * n)` time complexity, the
2235/// overall worst case time complexity for iteration is `O(m * n^2)`.
2236#[derive(Debug)]
2237pub struct Matches<'r, 'h> {
2238 haystack: &'h [u8],
2239 it: meta::FindMatches<'r, 'h>,
2240}
2241
2242impl<'r, 'h> Iterator for Matches<'r, 'h> {
2243 type Item = Match<'h>;
2244
2245 #[inline]
2246 fn next(&mut self) -> Option<Match<'h>> {
2247 self.it
2248 .next()
2249 .map(|sp| Match::new(self.haystack, sp.start(), sp.end()))
2250 }
2251
2252 #[inline]
2253 fn count(self) -> usize {
2254 // This can actually be up to 2x faster than calling `next()` until
2255 // completion, because counting matches when using a DFA only requires
2256 // finding the end of each match. But returning a `Match` via `next()`
2257 // requires the start of each match which, with a DFA, requires a
2258 // reverse forward scan to find it.
2259 self.it.count()
2260 }
2261}
2262
2263impl<'r, 'h> core::iter::FusedIterator for Matches<'r, 'h> {}
2264
2265/// An iterator over all non-overlapping capture matches in a haystack.
2266///
2267/// This iterator yields [`Captures`] values. The iterator stops when no more
2268/// matches can be found.
2269///
2270/// `'r` is the lifetime of the compiled regular expression and `'h` is the
2271/// lifetime of the matched string.
2272///
2273/// This iterator is created by [`Regex::captures_iter`].
2274///
2275/// # Time complexity
2276///
2277/// Note that since an iterator runs potentially many searches on the haystack
2278/// and since each search has worst case `O(m * n)` time complexity, the
2279/// overall worst case time complexity for iteration is `O(m * n^2)`.
2280#[derive(Debug)]
2281pub struct CaptureMatches<'r, 'h> {
2282 haystack: &'h [u8],
2283 it: meta::CapturesMatches<'r, 'h>,
2284}
2285
2286impl<'r, 'h> Iterator for CaptureMatches<'r, 'h> {
2287 type Item = Captures<'h>;
2288
2289 #[inline]
2290 fn next(&mut self) -> Option<Captures<'h>> {
2291 let static_captures_len = self.it.regex().static_captures_len();
2292 self.it.next().map(|caps| Captures {
2293 haystack: self.haystack,
2294 caps,
2295 static_captures_len,
2296 })
2297 }
2298
2299 #[inline]
2300 fn count(self) -> usize {
2301 // This can actually be up to 2x faster than calling `next()` until
2302 // completion, because counting matches when using a DFA only requires
2303 // finding the end of each match. But returning a `Match` via `next()`
2304 // requires the start of each match which, with a DFA, requires a
2305 // reverse forward scan to find it.
2306 self.it.count()
2307 }
2308}
2309
2310impl<'r, 'h> core::iter::FusedIterator for CaptureMatches<'r, 'h> {}
2311
2312/// An iterator over all substrings delimited by a regex match.
2313///
2314/// `'r` is the lifetime of the compiled regular expression and `'h` is the
2315/// lifetime of the byte string being split.
2316///
2317/// This iterator is created by [`Regex::split`].
2318///
2319/// # Time complexity
2320///
2321/// Note that since an iterator runs potentially many searches on the haystack
2322/// and since each search has worst case `O(m * n)` time complexity, the
2323/// overall worst case time complexity for iteration is `O(m * n^2)`.
2324#[derive(Debug)]
2325pub struct Split<'r, 'h> {
2326 haystack: &'h [u8],
2327 it: meta::Split<'r, 'h>,
2328}
2329
2330impl<'r, 'h> Iterator for Split<'r, 'h> {
2331 type Item = &'h [u8];
2332
2333 #[inline]
2334 fn next(&mut self) -> Option<&'h [u8]> {
2335 self.it.next().map(|span| &self.haystack[span])
2336 }
2337}
2338
2339impl<'r, 'h> core::iter::FusedIterator for Split<'r, 'h> {}
2340
2341/// An iterator over at most `N` substrings delimited by a regex match.
2342///
2343/// The last substring yielded by this iterator will be whatever remains after
2344/// `N-1` splits.
2345///
2346/// `'r` is the lifetime of the compiled regular expression and `'h` is the
2347/// lifetime of the byte string being split.
2348///
2349/// This iterator is created by [`Regex::splitn`].
2350///
2351/// # Time complexity
2352///
2353/// Note that since an iterator runs potentially many searches on the haystack
2354/// and since each search has worst case `O(m * n)` time complexity, the
2355/// overall worst case time complexity for iteration is `O(m * n^2)`.
2356///
2357/// Although note that the worst case time here has an upper bound given
2358/// by the `limit` parameter to [`Regex::splitn`].
2359#[derive(Debug)]
2360pub struct SplitN<'r, 'h> {
2361 haystack: &'h [u8],
2362 it: meta::SplitN<'r, 'h>,
2363}
2364
2365impl<'r, 'h> Iterator for SplitN<'r, 'h> {
2366 type Item = &'h [u8];
2367
2368 #[inline]
2369 fn next(&mut self) -> Option<&'h [u8]> {
2370 self.it.next().map(|span| &self.haystack[span])
2371 }
2372
2373 #[inline]
2374 fn size_hint(&self) -> (usize, Option<usize>) {
2375 self.it.size_hint()
2376 }
2377}
2378
2379impl<'r, 'h> core::iter::FusedIterator for SplitN<'r, 'h> {}
2380
2381/// An iterator over the names of all capture groups in a regex.
2382///
2383/// This iterator yields values of type `Option<&str>` in order of the opening
2384/// capture group parenthesis in the regex pattern. `None` is yielded for
2385/// groups with no name. The first element always corresponds to the implicit
2386/// and unnamed group for the overall match.
2387///
2388/// `'r` is the lifetime of the compiled regular expression.
2389///
2390/// This iterator is created by [`Regex::capture_names`].
2391#[derive(Clone, Debug)]
2392pub struct CaptureNames<'r>(captures::GroupInfoPatternNames<'r>);
2393
2394impl<'r> Iterator for CaptureNames<'r> {
2395 type Item = Option<&'r str>;
2396
2397 #[inline]
2398 fn next(&mut self) -> Option<Option<&'r str>> {
2399 self.0.next()
2400 }
2401
2402 #[inline]
2403 fn size_hint(&self) -> (usize, Option<usize>) {
2404 self.0.size_hint()
2405 }
2406
2407 #[inline]
2408 fn count(self) -> usize {
2409 self.0.count()
2410 }
2411}
2412
2413impl<'r> ExactSizeIterator for CaptureNames<'r> {}
2414
2415impl<'r> core::iter::FusedIterator for CaptureNames<'r> {}
2416
2417/// An iterator over all group matches in a [`Captures`] value.
2418///
2419/// This iterator yields values of type `Option<Match<'h>>`, where `'h` is the
2420/// lifetime of the haystack that the matches are for. The order of elements
2421/// yielded corresponds to the order of the opening parenthesis for the group
2422/// in the regex pattern. `None` is yielded for groups that did not participate
2423/// in the match.
2424///
2425/// The first element always corresponds to the implicit group for the overall
2426/// match. Since this iterator is created by a [`Captures`] value, and a
2427/// `Captures` value is only created when a match occurs, it follows that the
2428/// first element yielded by this iterator is guaranteed to be non-`None`.
2429///
2430/// The lifetime `'c` corresponds to the lifetime of the `Captures` value that
2431/// created this iterator, and the lifetime `'h` corresponds to the originally
2432/// matched haystack.
2433#[derive(Clone, Debug)]
2434pub struct SubCaptureMatches<'c, 'h> {
2435 haystack: &'h [u8],
2436 it: captures::CapturesPatternIter<'c>,
2437}
2438
2439impl<'c, 'h> Iterator for SubCaptureMatches<'c, 'h> {
2440 type Item = Option<Match<'h>>;
2441
2442 #[inline]
2443 fn next(&mut self) -> Option<Option<Match<'h>>> {
2444 self.it.next().map(|group| {
2445 group.map(|sp| Match::new(self.haystack, sp.start, sp.end))
2446 })
2447 }
2448
2449 #[inline]
2450 fn size_hint(&self) -> (usize, Option<usize>) {
2451 self.it.size_hint()
2452 }
2453
2454 #[inline]
2455 fn count(self) -> usize {
2456 self.it.count()
2457 }
2458}
2459
2460impl<'c, 'h> ExactSizeIterator for SubCaptureMatches<'c, 'h> {}
2461
2462impl<'c, 'h> core::iter::FusedIterator for SubCaptureMatches<'c, 'h> {}
2463
2464/// A trait for types that can be used to replace matches in a haystack.
2465///
2466/// In general, users of this crate shouldn't need to implement this trait,
2467/// since implementations are already provided for `&[u8]` along with other
2468/// variants of byte string types, as well as `FnMut(&Captures) -> Vec<u8>` (or
2469/// any `FnMut(&Captures) -> T` where `T: AsRef<[u8]>`). Those cover most use
2470/// cases, but callers can implement this trait directly if necessary.
2471///
2472/// # Example
2473///
2474/// This example shows a basic implementation of the `Replacer` trait. This can
2475/// be done much more simply using the replacement byte string interpolation
2476/// support (e.g., `$first $last`), but this approach avoids needing to parse
2477/// the replacement byte string at all.
2478///
2479/// ```
2480/// use regex::bytes::{Captures, Regex, Replacer};
2481///
2482/// struct NameSwapper;
2483///
2484/// impl Replacer for NameSwapper {
2485/// fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2486/// dst.extend_from_slice(&caps["first"]);
2487/// dst.extend_from_slice(b" ");
2488/// dst.extend_from_slice(&caps["last"]);
2489/// }
2490/// }
2491///
2492/// let re = Regex::new(r"(?<last>[^,\s]+),\s+(?<first>\S+)").unwrap();
2493/// let result = re.replace(b"Springsteen, Bruce", NameSwapper);
2494/// assert_eq!(result, &b"Bruce Springsteen"[..]);
2495/// ```
2496pub trait Replacer {
2497 /// Appends possibly empty data to `dst` to replace the current match.
2498 ///
2499 /// The current match is represented by `caps`, which is guaranteed to have
2500 /// a match at capture group `0`.
2501 ///
2502 /// For example, a no-op replacement would be
2503 /// `dst.extend_from_slice(&caps[0])`.
2504 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>);
2505
2506 /// Return a fixed unchanging replacement byte string.
2507 ///
2508 /// When doing replacements, if access to [`Captures`] is not needed (e.g.,
2509 /// the replacement byte string does not need `$` expansion), then it can
2510 /// be beneficial to avoid finding sub-captures.
2511 ///
2512 /// In general, this is called once for every call to a replacement routine
2513 /// such as [`Regex::replace_all`].
2514 fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>> {
2515 None
2516 }
2517
2518 /// Returns a type that implements `Replacer`, but that borrows and wraps
2519 /// this `Replacer`.
2520 ///
2521 /// This is useful when you want to take a generic `Replacer` (which might
2522 /// not be cloneable) and use it without consuming it, so it can be used
2523 /// more than once.
2524 ///
2525 /// # Example
2526 ///
2527 /// ```
2528 /// use regex::bytes::{Regex, Replacer};
2529 ///
2530 /// fn replace_all_twice<R: Replacer>(
2531 /// re: Regex,
2532 /// src: &[u8],
2533 /// mut rep: R,
2534 /// ) -> Vec<u8> {
2535 /// let dst = re.replace_all(src, rep.by_ref());
2536 /// let dst = re.replace_all(&dst, rep.by_ref());
2537 /// dst.into_owned()
2538 /// }
2539 /// ```
2540 fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self> {
2541 ReplacerRef(self)
2542 }
2543}
2544
2545impl<'a, const N: usize> Replacer for &'a [u8; N] {
2546 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2547 caps.expand(&**self, dst);
2548 }
2549
2550 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2551 no_expansion(self)
2552 }
2553}
2554
2555impl<const N: usize> Replacer for [u8; N] {
2556 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2557 caps.expand(&*self, dst);
2558 }
2559
2560 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2561 no_expansion(self)
2562 }
2563}
2564
2565impl<'a> Replacer for &'a [u8] {
2566 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2567 caps.expand(*self, dst);
2568 }
2569
2570 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2571 no_expansion(self)
2572 }
2573}
2574
2575impl<'a> Replacer for &'a Vec<u8> {
2576 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2577 caps.expand(*self, dst);
2578 }
2579
2580 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2581 no_expansion(self)
2582 }
2583}
2584
2585impl Replacer for Vec<u8> {
2586 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2587 caps.expand(self, dst);
2588 }
2589
2590 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2591 no_expansion(self)
2592 }
2593}
2594
2595impl<'a> Replacer for Cow<'a, [u8]> {
2596 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2597 caps.expand(self.as_ref(), dst);
2598 }
2599
2600 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2601 no_expansion(self)
2602 }
2603}
2604
2605impl<'a> Replacer for &'a Cow<'a, [u8]> {
2606 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2607 caps.expand(self.as_ref(), dst);
2608 }
2609
2610 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2611 no_expansion(self)
2612 }
2613}
2614
2615impl<F, T> Replacer for F
2616where
2617 F: FnMut(&Captures<'_>) -> T,
2618 T: AsRef<[u8]>,
2619{
2620 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2621 dst.extend_from_slice((*self)(caps).as_ref());
2622 }
2623}
2624
2625/// A by-reference adaptor for a [`Replacer`].
2626///
2627/// This permits reusing the same `Replacer` value in multiple calls to a
2628/// replacement routine like [`Regex::replace_all`].
2629///
2630/// This type is created by [`Replacer::by_ref`].
2631#[derive(Debug)]
2632pub struct ReplacerRef<'a, R: ?Sized>(&'a mut R);
2633
2634impl<'a, R: Replacer + ?Sized + 'a> Replacer for ReplacerRef<'a, R> {
2635 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2636 self.0.replace_append(caps, dst)
2637 }
2638
2639 fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>> {
2640 self.0.no_expansion()
2641 }
2642}
2643
2644/// A helper type for forcing literal string replacement.
2645///
2646/// It can be used with routines like [`Regex::replace`] and
2647/// [`Regex::replace_all`] to do a literal string replacement without expanding
2648/// `$name` to their corresponding capture groups. This can be both convenient
2649/// (to avoid escaping `$`, for example) and faster (since capture groups
2650/// don't need to be found).
2651///
2652/// `'s` is the lifetime of the literal string to use.
2653///
2654/// # Example
2655///
2656/// ```
2657/// use regex::bytes::{NoExpand, Regex};
2658///
2659/// let re = Regex::new(r"(?<last>[^,\s]+),\s+(\S+)").unwrap();
2660/// let result = re.replace(b"Springsteen, Bruce", NoExpand(b"$2 $last"));
2661/// assert_eq!(result, &b"$2 $last"[..]);
2662/// ```
2663#[derive(Clone, Debug)]
2664pub struct NoExpand<'s>(pub &'s [u8]);
2665
2666impl<'s> Replacer for NoExpand<'s> {
2667 fn replace_append(&mut self, _: &Captures<'_>, dst: &mut Vec<u8>) {
2668 dst.extend_from_slice(self.0);
2669 }
2670
2671 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2672 Some(Cow::Borrowed(self.0))
2673 }
2674}
2675
2676/// Quickly checks the given replacement string for whether interpolation
2677/// should be done on it. It returns `None` if a `$` was found anywhere in the
2678/// given string, which suggests interpolation needs to be done. But if there's
2679/// no `$` anywhere, then interpolation definitely does not need to be done. In
2680/// that case, the given string is returned as a borrowed `Cow`.
2681///
2682/// This is meant to be used to implement the `Replacer::no_expansion` method
2683/// in its various trait impls.
2684fn no_expansion<T: AsRef<[u8]>>(replacement: &T) -> Option<Cow<'_, [u8]>> {
2685 let replacement = replacement.as_ref();
2686 match crate::find_byte::find_byte(b'$', replacement) {
2687 Some(_) => None,
2688 None => Some(Cow::Borrowed(replacement)),
2689 }
2690}
2691
2692#[cfg(test)]
2693mod tests {
2694 use super::*;
2695 use alloc::format;
2696
2697 #[test]
2698 fn test_match_properties() {
2699 let haystack = b"Hello, world!";
2700 let m = Match::new(haystack, 7, 12);
2701
2702 assert_eq!(m.start(), 7);
2703 assert_eq!(m.end(), 12);
2704 assert_eq!(m.is_empty(), false);
2705 assert_eq!(m.len(), 5);
2706 assert_eq!(m.as_bytes(), b"world");
2707 }
2708
2709 #[test]
2710 fn test_empty_match() {
2711 let haystack = b"";
2712 let m = Match::new(haystack, 0, 0);
2713
2714 assert_eq!(m.is_empty(), true);
2715 assert_eq!(m.len(), 0);
2716 }
2717
2718 #[test]
2719 fn test_debug_output_valid_utf8() {
2720 let haystack = b"Hello, world!";
2721 let m = Match::new(haystack, 7, 12);
2722 let debug_str = format!("{m:?}");
2723
2724 assert_eq!(
2725 debug_str,
2726 r#"Match { start: 7, end: 12, bytes: "world" }"#
2727 );
2728 }
2729
2730 #[test]
2731 fn test_debug_output_invalid_utf8() {
2732 let haystack = b"Hello, \xFFworld!";
2733 let m = Match::new(haystack, 7, 13);
2734 let debug_str = format!("{m:?}");
2735
2736 assert_eq!(
2737 debug_str,
2738 r#"Match { start: 7, end: 13, bytes: "\xffworld" }"#
2739 );
2740 }
2741
2742 #[test]
2743 fn test_debug_output_various_unicode() {
2744 let haystack =
2745 "Hello, 😊 world! 안녕하세요? Ù…Ø±ØØ¨Ø§ بالعالم!".as_bytes();
2746 let m = Match::new(haystack, 0, haystack.len());
2747 let debug_str = format!("{m:?}");
2748
2749 assert_eq!(
2750 debug_str,
2751 r#"Match { start: 0, end: 62, bytes: "Hello, 😊 world! 안녕하세요? Ù…Ø±ØØ¨Ø§ بالعالم!" }"#
2752 );
2753 }
2754
2755 #[test]
2756 fn test_debug_output_ascii_escape() {
2757 let haystack = b"Hello,\tworld!\nThis is a \x1b[31mtest\x1b[0m.";
2758 let m = Match::new(haystack, 0, haystack.len());
2759 let debug_str = format!("{m:?}");
2760
2761 assert_eq!(
2762 debug_str,
2763 r#"Match { start: 0, end: 38, bytes: "Hello,\tworld!\nThis is a \u{1b}[31mtest\u{1b}[0m." }"#
2764 );
2765 }
2766
2767 #[test]
2768 fn test_debug_output_match_in_middle() {
2769 let haystack = b"The quick brown fox jumps over the lazy dog.";
2770 let m = Match::new(haystack, 16, 19);
2771 let debug_str = format!("{m:?}");
2772
2773 assert_eq!(debug_str, r#"Match { start: 16, end: 19, bytes: "fox" }"#);
2774 }
2775}