0

The search query is : ([\s]*print)[^(](.*)

It matches, BUT when I put ^ and $ like this : ^([\s]*print)[^(](.*)$, it doesn't.

Again both work in search-forward-regex but the 2nd one does not work in replace-regex. I was curious about this behavior

  • 1
    Down-voting because the question is useless when you haven't shown what you're trying to match. The regexp matches what it should match. We've no idea what you think it should match. – phils Oct 10 '17 at 9:46
  • If you pay attention, I mean whatever matches is not the main concern. Once it matches, it should redundantly match when I put ^ in the beginning and $ at the end. The match failed when I did so - - this was confusing to me. I was trying to match:" print whatever\n" -- \n meaning newline. Such sentences where the beginning may have whitespace, and end is newline. Thanks! – user1782328 Oct 10 '17 at 11:00
  • "where the beginning may have whitespace" will not match ^( so that's one problem/misunderstanding, and why I said you needed to provide this information. – phils Oct 10 '17 at 11:17
  • I think there is additional confusion, mind you. ([\s]print) matches the text (\print) and (sprint), and I'm not sure that's your intent. (n.b. I'm assuming you're calling search-forward-regexp interactively). – phils Oct 10 '17 at 11:30
  • Oh I am so sorry. I understand now -- I am new to this. After I used \s-* for whitespace it worked. I used [^(] to mean that immediately after print there should not be (. Because I had two kinds of print: print("x") and print "x". Wanted to replace the former. I did not have any other format of print like print (), etc so I used very strict match. Nevamind it works now. I could edit the question but that would make the answers misleading so I will leave it like that! thanks again. – user1782328 Oct 13 '17 at 8:29
3

What do you mean by [\s]*? If you want to match zero or more whitespace in Emacs, use

\s-*

as documented in Backslash in Regular Expressions.

For example, the regex

^\(\s-*print\)[^(]\(.*\)$

matches

 printf(blah)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.