Skip to content

Commit 99d52ee

Browse files
ravidahiya-googlecopybara-github
authored andcommitted
Document regex syntax for contains_regex and matches_regex.
This CL documents that the regex matchers use the standard Rust `regex` crate, and clarifies the syntax they accept. It also adds a warning about using `(?m)` with `matches_regex`. PUBLIC: Document regex syntax for contains_regex and matches_regex. PiperOrigin-RevId: 920782797
1 parent 6b1a9c0 commit 99d52ee

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

googletest/src/matchers/contains_regex_matcher.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ use std::ops::Deref;
4444
/// # should_pass_2().unwrap();
4545
/// ```
4646
///
47+
/// # Regular Expression Syntax
48+
///
49+
/// The regular expression syntax matches that of the [`regex` crate](https://docs.rs/regex).
50+
/// This is a linear-time regular expression engine which does not support lookarounds
51+
/// or backreferences.
52+
///
53+
/// To enable multi-line matching (where `^` and `$` match line boundaries instead of
54+
/// the start and end of the entire input), prefix the pattern with `(?m)`.
55+
///
56+
/// For details on the supported syntax, see the
57+
/// [regex crate documentation](https://docs.rs/regex/latest/regex/#syntax).
58+
///
59+
/// # Panics
60+
///
4761
/// Panics if the given `pattern` is not a syntactically valid regular
4862
/// expression.
4963
#[track_caller]

googletest/src/matchers/matches_regex_matcher.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ use std::ops::Deref;
5252
/// # should_pass_2().unwrap();
5353
/// ```
5454
///
55+
/// # Regular Expression Syntax
56+
///
57+
/// The regular expression syntax matches that of the [`regex` crate](https://docs.rs/regex).
58+
/// This is a linear-time regular expression engine which does not support lookarounds
59+
/// or backreferences.
60+
///
61+
/// For details on the supported syntax, see the
62+
/// [regex crate documentation](https://docs.rs/regex/latest/regex/#syntax).
63+
///
64+
/// Note that `matches_regex` automatically wraps the pattern with `^` and `$`
65+
/// to ensure it matches the entire string.
66+
///
67+
/// ## Multi-line matching
68+
///
69+
/// If you want to match a multi-line string where `.` matches newlines, use the
70+
/// `(?s)` flag (e.g., `(?s)begin.*end`).
71+
///
72+
/// Avoid using the `(?m)` flag (multi-line mode) with `matches_regex`, as it
73+
/// redefines `^` and `$` to match line boundaries. Because `matches_regex`
74+
/// wraps your pattern in `^...$` and uses `is_match` internally, enabling
75+
/// `(?m)` will cause the matcher to succeed if *any* single line matches the
76+
/// pattern, rather than requiring the entire string to match. If you want to
77+
/// match a pattern against individual lines of a multi-line string, use
78+
/// [`contains_regex`][crate::matchers::contains_regex] with `(?m)` instead.
79+
///
80+
/// # Panics
81+
///
5582
/// Panics if the given `pattern` is not a syntactically valid regular
5683
/// expression.
5784
// N.B. This returns the concrete type rather than an impl Matcher so that it

0 commit comments

Comments
 (0)