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.

phils
  • 48,657
  • 3
  • 76
  • 115
147pm
  • 2,907
  • 1
  • 18
  • 39
  • 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 06 '20 at 13:21
  • BTW, `isearch` itself can look for a space delimited `foo` by just typing the literal spaces around `foo`. – NickD Mar 06 '20 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 07 '20 at 05:08
  • 1
    In one of those you used `"\s-"` and in the other you used `"\\s-"`. See https://emacs.stackexchange.com/a/55604/454 regarding backslashes in double-quoted strings. – phils Mar 07 '20 at 05:52

1 Answers1

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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 06 '20 at 00: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 06 '20 at 01:32
  • IOW, try `M-x foo`. Does that not do what you say/ask? – Drew Mar 06 '20 at 22:25
  • I recast the question to be more specific to my main confusion: What is `regexp-quote` really doing? – 147pm Mar 07 '20 at 05: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 07 '20 at 05: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 07 '20 at 05:41
  • 1
    And FYI https://emacs.stackexchange.com/q/13241/454 is such a duplicate. – phils Mar 07 '20 at 05:46
  • @phils Emacs' documentation is often no more than a reference dump (Exhibit A: org-mode "official" manual), leaving all of us non-Sheldon Cooper types to "guess-and-test" until something works. Or to Google around through the universe of "addenda" made by sympathetic people in lieu of real O'Reilly-style documentation. So why don't you and all the other mods on this site treat a questioner as a lost and frightened child, and not as an oaf tracking mud on their carpet, okay? – 147pm Mar 07 '20 at 15:43
  • Well I don't understand the criticism. I was polite in asking you to keep separate questions separate; I tidied things up for you; and I then provided you with two links (and not even to the Emacs documentation) one of which explained some further confusion in your question, and the other of which directly answered what had turned out to be your *real* question. Which of those actions has caused you such distress? – phils Mar 07 '20 at 22:58