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.

phils
  • 48,657
  • 3
  • 76
  • 115
Edman
  • 1,167
  • 7
  • 13
  • 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 May 20 '23 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 May 20 '23 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 May 20 '23 at 14:51

1 Answers1

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.

phils
  • 48,657
  • 3
  • 76
  • 115
  • 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 May 22 '23 at 05: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-quote`s 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 May 22 '23 at 08: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, https://emacs.stackexchange.com/a/5577/454 should help to clarify. – phils May 22 '23 at 08:44