0

What function should I call to read a word or regular expression from the minibuffer? Am writing a function that accepts a word or regular expression and counts the number of matches.

But need to change (char-to-string word) so the code can match a regular expression rather than a char.

    (defun rk-regex-count (regex reg-beg reg-end)
      "Count times a regular expression exists.
    
    REGEX     Regular expression.
    REG-BEG   Marker defining start of region.
    REG-END   Marker defining end of region.
    
    Usage
          M-x rk-regex-count"
    
  (interactive
   (list (read-regexp "Regex: ")
     (region-beginning)
     (region-end) ))

  (save-excursion
    (let ( (a 0) )

      (goto-char reg-beg)
      (while (and (< (point) reg-end)
                  (re-search-forward regex reg-end t))
        (cl-incf a))

      (message "Marks: %s Count: %d" open a)
      (goto-char reg-beg))) )
5
  • A regular expression is represented by a string, and so is a word (however you define that). Read the section in the Elisp manual entitled Reading Text Strings with the Minibuffer.
    – NickD
    Mar 21 at 0:30
  • I see three functions that can be used read-from-minibuffer, read-string, read-regexp, but having difficulty deciding what's the appropriate to use.
    – Veak
    Mar 21 at 1:22
  • Your question is too vague - it allows no way to guess which of those functions might be more appropriate for your use case. So you answered your own question in your comment: each of those functions works.
    – Drew
    Mar 21 at 1:28
  • Now the question does not ask a question :-) If the stuff you added to the question (the second and third paragraphs and the code) is supposed to answer the question, then you should put all that into an answer (you can answer your own question). Just cut-and-paste it into the Your Answer box below and delete it from the question.
    – NickD
    Mar 21 at 7:31
  • The frustrating part is that SE customarily disallows a user to answer the question.
    – Veak
    Mar 21 at 23:38

1 Answer 1

1

There are three basic functions that can be used read-from-minibuffer, read-string, and read-regexp. The advantage of read-regexp is that it does not require escaping regular expressions as strings require.

1
  • Notwithstanding that you should use read-regexp for reading a regular expression, I don't believe there are any "escaping" benefits over read-string. In both cases the user gets to input a string verbatim rather than in string read syntax. For the listed functions, you would only be using string read syntax if you used read-from-minibuffer with a non-nil READ argument.
    – phils
    Mar 23 at 0:06

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.