I'm trying to use replace-regexp to change the following code
samples <- rep(10, 5)*1000 ## steps determined by samples * thin
adaptive.ratio <- c(rep(c(1,0), 2), 0) ## Alternate between adaptive and non-adaptive
...
to
print(paste0("samples: ", samples)) ## steps determined by samples * thin
print(paste0("adaptive.ratio: ", adaptive.ratio)) ## Alternate between adaptive and non-adaptive
...
I am using the following regexp to do so
^\(\b.*\b\)\([^#]*\) → print(paste0("\1: ", \1) ) \2
But this highlights
samples <- rep(10, 5)*1000 ## steps determined by samples * thin
adaptive.ratio <- c(rep(c(1,0), 2), 0)
for the first replacement rather than what I want, which is to highlight only
print(paste0("samples: ", samples)) ## steps determined by samples * thin
I've tried escaping the # sign with #, adding a space between the two sets of parentheses, but that doesn't help.
I realize this is somehow related to the greedy nature of regexp matching, but I don't understand what am I doing wrong nor, as a result, how to fix it.
\bmatching more than the first set of characters, it's due to the.*in between them which should match anything, including things that aren't word boundaries. I still don't understand, however, why the matching goes beyond the the new line – mikemtnbikes Dec 15 '21 at 20:37.*matches the maximum. Non-greedy.*?matches the minimum. Although you would want\b.+?\brather than\b.*?\bto avoid matching the same\btwice. – phils Dec 15 '21 at 21:26.does not match newlines,[^#]does. – phils Dec 15 '21 at 21:31M-x re-builderbtw. It's a good visualiser, especially when the regexp contains groups. SeeM-x finder-commentary RET re-builderfor a description, and https://emacs.stackexchange.com/q/5568 may be useful reading as well. – phils Dec 15 '21 at 21:41