@@ -161,6 +161,7 @@ impl StringOp {
161
161
match first {
162
162
Some ( '^' ) => { } // Start of string anchor is already added
163
163
Some ( '*' ) => re_string. push_str ( r"\*" ) ,
164
+ Some ( '$' ) if !is_end_of_expression ( & pattern_chars) => re_string. push_str ( r"\$" ) ,
164
165
Some ( '\\' ) if right. len ( ) == 1 => return Err ( ExprError :: TrailingBackslash ) ,
165
166
Some ( char) => re_string. push ( char) ,
166
167
None => return Ok ( 0 . into ( ) ) ,
@@ -185,23 +186,12 @@ impl StringOp {
185
186
_ => re_string. push_str ( r"\^" ) ,
186
187
} ,
187
188
'$' => {
188
- if let Some ( '\\' ) = pattern_chars. peek ( ) {
189
- // The next character was checked to be a backslash
190
- let backslash = pattern_chars. next ( ) . unwrap_or_default ( ) ;
191
- match pattern_chars. peek ( ) {
192
- // End of a capturing group
193
- Some ( ')' ) => re_string. push ( '$' ) ,
194
- // End of an alternative pattern
195
- Some ( '|' ) => re_string. push ( '$' ) ,
196
- _ => re_string. push_str ( r"\$" ) ,
197
- }
198
- re_string. push ( backslash) ;
199
- } else if ( prev_is_escaped || prev != '\\' )
200
- && pattern_chars. peek ( ) . is_some ( )
201
- {
189
+ if is_end_of_expression ( & pattern_chars) {
190
+ re_string. push ( curr) ;
191
+ } else if !curr_is_escaped {
202
192
re_string. push_str ( r"\$" ) ;
203
193
} else {
204
- re_string. push ( '$' ) ;
194
+ re_string. push ( curr ) ;
205
195
}
206
196
}
207
197
'\\' if !curr_is_escaped && pattern_chars. peek ( ) . is_none ( ) => {
@@ -247,6 +237,25 @@ impl StringOp {
247
237
}
248
238
}
249
239
240
+ /// Check if regex pattern character iterator is at the end of a regex expression or subexpression
241
+ fn is_end_of_expression < I > ( pattern_chars : & I ) -> bool
242
+ where
243
+ I : Iterator < Item = char > + Clone ,
244
+ {
245
+ let mut pattern_chars_clone = pattern_chars. clone ( ) ;
246
+ match pattern_chars_clone. next ( ) {
247
+ Some ( '\\' ) => {
248
+ match pattern_chars_clone. next ( ) {
249
+ Some ( ')' ) // End of a capturing group
250
+ | Some ( '|' ) => true , // End of an alternative pattern
251
+ _ => false ,
252
+ }
253
+ }
254
+ None => true , // No characters left
255
+ _ => false ,
256
+ }
257
+ }
258
+
250
259
/// Check for errors in a supplied regular expression
251
260
///
252
261
/// GNU coreutils shows messages for invalid regular expressions
0 commit comments