1

I'm looking at this

One use of regexp-quote is to combine an exact string match with context described as a regular expression. For example, this searches for the string that is the value of string, surrounded by whitespace:

(re-search-forward  (concat "\\s-" (regexp-quote string) "\\s-"))

and wondering how to adapt it to do simply go through the file and search for a word that is surrounded by one or more spaces, i.e., whitespace. I'd like to do it as a regex search in the minibuffer. Tried Search/Regex Forward from the menu, also M-x : to type it as a command in the minibuffer -- all to no avail. Once again, I'm lost in a "guess-and-test" loop.

  • Are you looking for isearch-forward-regexp by any chance? It is bound to C-M-s and works just like isearch except that your input is treated as a regexp instead of a string. You can type \s-foo\s- to it to search for whitespace delimited foo. – NickD Mar 6 at 13:21
  • BTW, isearch itself can look for a space delimited foo by just typing the literal spaces around foo. – NickD Mar 6 at 13:23
  • Yes, thanks, NickD, that's what I was looking for, i.e., the isearch-forward-regexp. In my OP, I tried (re-search-forward (concat "\s-" "word" "\s-")) with M-: in the command line -- to no avail. Then I tried (re-search-forward (concat "\\s-" (regexp-quote "word") "\\s-")) which did work. A learning experience.... – 147pm Mar 7 at 5:08
  • 1
    In one of those you used "\s-" and in the other you used "\\s-". See emacs.stackexchange.com/a/55604/454 regarding backslashes in double-quoted strings. – phils Mar 7 at 5:52
2

Not quite sure what you're looking for. This searches for a word surrounded by whitespace. User input for read-string is tested to be sure it is a word.

(defun foo (word)
  (interactive
   (let ((strg  (read-string "Word: ")))
     (while (not (string-match-p "\\`\\w+\\'" strg)) ; Not a word.  Prompt again.
       (setq strg  (read-string "Word: ")))
     (list strg)))
  (re-search-forward (concat "\\s-" (regexp-quote word) "\\s-")))

But if you're just wanting to search for a word interactively (not necessarily surrounded by whitespace), you can use word-search-forward or word-search-forward-lax, or for incremental search, just M-s w.

| improve this answer | |
  • I was looking for how to run regex in the minibuffer with M-x: , i.e., how would I do the task I mentioned above. – 147pm Mar 6 at 0:49
  • You mean that you want to use M-x to invoke a command that does something. What, exactly, is not too clear. If you want to search for a word that you input then the code in this answer should do that. – Drew Mar 6 at 1:32
  • 1
    @147pm Well you've now changed the Question into something very different from what it was originally, thus rendering the existing Answer irrelevant. Please don't do that. You should instead ask a NEW question. – phils Mar 7 at 5:39
  • 1
    @147pm I have rolled back that last edit to the Question. Please ask separately about regexp-quote (if there isn't an existing duplicate). – phils Mar 7 at 5:41
  • 1
    And FYI emacs.stackexchange.com/q/13241/454 is such a duplicate. – phils Mar 7 at 5:46

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.