@@ -168,8 +168,8 @@ pub struct Opts {
168168 pub regex : bool ,
169169
170170 /// Treat the pattern as a literal string instead of a regular expression. Note
171- /// that this also performs substring comparison . If you want to match on an
172- /// exact filename, consider using '--glob' .
171+ /// that the pattern would still match on a substring of the input . If you want
172+ /// to match on an exact filename, consider adding '--anchor=input' as well .
173173 #[ arg(
174174 long,
175175 short = 'F' ,
@@ -246,6 +246,20 @@ pub struct Opts {
246246 ) ]
247247 pub full_path : bool ,
248248
249+ /// By default, the search pattern for --regex and --fixed-strings can match any part of the input.
250+ /// (See the --full-path option for what constitutes input)
251+ ///
252+ /// This flag allows other ways to anchor the pattern.
253+ ///
254+ /// Conflicts with the --glob flag: globs always match the entire input
255+ #[ arg(
256+ long,
257+ help = "Where to anchor the pattern" ,
258+ conflicts_with( "glob" ) ,
259+ long_help
260+ ) ]
261+ pub anchor : Option < Anchor > ,
262+
249263 /// Separate search results by the null character (instead of newlines).
250264 /// Useful for piping results to 'xargs'.
251265 #[ arg(
@@ -680,6 +694,17 @@ impl Opts {
680694 self . rg_alias_hidden_ignore > 0
681695 }
682696
697+ pub fn anchor ( & self ) -> Option < Anchor > {
698+ if self . glob {
699+ // globset has no way to use an anchor.
700+ // Otherwise we'd guard like this:
701+ // && !self.no_anchor && self.anchor.is_none()
702+ Some ( Anchor :: Input )
703+ } else {
704+ self . anchor
705+ }
706+ }
707+
683708 pub fn max_depth ( & self ) -> Option < usize > {
684709 self . max_depth . or ( self . exact_depth )
685710 }
@@ -725,6 +750,14 @@ fn default_num_threads() -> NonZeroUsize {
725750 . min ( limit)
726751}
727752
753+ #[ derive( Copy , Clone , PartialEq , Eq , ValueEnum ) ]
754+ pub enum Anchor {
755+ InputStart ,
756+ InputEnd ,
757+ Input ,
758+ Word ,
759+ }
760+
728761#[ derive( Copy , Clone , PartialEq , Eq , ValueEnum ) ]
729762pub enum FileType {
730763 #[ value( alias = "f" ) ]
0 commit comments