0

I wanted to create a regex to find these dates:

1966/08/20
2023/02/12

In re-builder I was able to build this:

"\([0-9]\{4\}\)/\([0-9]\{2\}\)/\([0-9]\{2\}\)"

In the buffer the dates are picked up and highlighted.

If I copy this exact same string and invoke isearch-forward-regexp Emacs does not find the dates. However, if I copy the actual regex into the buffer isearch-forward-regexp finds that.

3
  • 1
    re-builder provides for different syntax (read, string, rx) (see the docs for explanation). Depending on the syntax, the grouping parens and the multiplier braces have to be preceded by either a single (for string syntax) or a double (for read syntax) backslash to match. I cannot get any matches without them, so I'm not sure how you manage to get matches in re-builder.
    – NickD
    Commented May 20, 2023 at 12:30
  • [0-9]\{4\}/[0-9]\{2\}/[0-9]\{2\} works (i.e. without the capturing group). As to re-builder I have reb-re-syntax set to 'string. The double escapes made no difference.
    – Edman
    Commented May 20, 2023 at 12:51
  • 2
    You should fix the question. Yes, the capturing groups don't matter for matching, but they do matter when replacing; but if you are going to use them, then use \(... \): plain parens are not special - they just match themselves.
    – NickD
    Commented May 20, 2023 at 14:51

1 Answer 1

2

Your question is unclear.

If I copy this exact same string and invoke isearch-forward-regexp Emacs does not find the dates.

What do you mean by that?

Isearch doesn't automatically search for the last string you copied.

If you mean (and this is my best guess) that you typed C-M-sC-y then that's doing a regexp-quote on the copied text to search for it verbatim (but you would see this in the modified regexp shown in the minibuffer).

If it's not that, then tell us specifically what you are doing (give exact keystrokes), what you are expecting, and what's actually happening?


Edit: The comments confirm that the above guess was accurate, so the solution is as follows...

Rather than doing this: C-M-sC-y (which results in the pasted text being passed through regexp-quote)

Instead, do this: C-M-sM-eC-yRET

I.e.: Call isearch-edit-string to edit the search pattern in the minibuffer and then yank the copied text.

3
  • I mean this: I am copying a (successful) regexp from the re-builder buffer (and copying without the quotes). Then I close re-builder and invoke C-M-s and yank the copied text using C-y, but Regexp I-search finds nothing.
    – Edman
    Commented May 22, 2023 at 5:03
  • Ok, so that is what I'd guessed, and therefore the reason is what I indicated already -- C-y in an isearch is for finding the pasted text verbatim and so it regexp-quotes the text (which you will observe in the search pattern shown in the echo area). Instead of doing that, you need to type M-e to edit the search pattern, and then C-y to paste the regexp (and RET to resume searching).
    – phils
    Commented May 22, 2023 at 8:41
  • Also, make sure you have re-builder set to string syntax rather than read syntax, otherwise you'll be trying to use the read syntax for elisp code interactively, which will fail as soon as backslashes are involved (amongst some other differences). From your question, it looks like you are already doing this, but I mention it just to be sure. If you're unsure about this side of things, emacs.stackexchange.com/a/5577/454 should help to clarify.
    – phils
    Commented May 22, 2023 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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