0

I'm using a regular expression search which is working as expected.

This searches for unified diff file entries and works as expected.

(re-search-forward
  (concat
    ;; Prefix '+++ '.
    "^"
    "\\-\\-\\-[[:blank:]]+.*\n"
    "\\+\\+\\+[[:blank:]]+.*\n"
    ;; May have trailing text which can be safely ignored.
    "@@[[:blank:]]+.*[[:blank:]]@@")
  nil t 1)

However some diffs have leading lines, eg:

 // some context text.
diff --git a/my_source.c b/my_source.c
index 43484a57f1c..5985d498606 100644
--- a/my_source.c
+++ b/my_source.c
 // some other context text.

My question is:

How can I include lines in the search which may or may not exist?

In this case: diff and index.

I tried this but it doesn't work, the \n literals prevent the following ^ from being detected.

(re-search-forward
  (concat
    ;; This fails :(
    "\\(\\|^diff[[:blank:]]+.*\n\\)"
    "\\(\\|^index[[:blank:]]+.*\n\\)"
    ;; Prefix '+++ '.
    "^"
    "\\-\\-\\-[[:blank:]]+.*\n"
    "\\+\\+\\+[[:blank:]]+.*\n"
    ;; May have trailing text which can be safely ignored.
    "@@[[:blank:]]+.*[[:blank:]]@@")
  nil t 1)

How can these lines be included in the search if they exist, otherwise left as empty groups if they aren't present?

1 Answer 1

2

? is the zero-or-one quantifier: \\(...\\)? says the group may or may not match anything.

Make it non-capturing if you don't care about backreferences to that group: \\(?:...\\)?

I tried this but it doesn't work, the \n literals prevent the following ^ from being detected.

^ only has its special meaning in certain positions; You can't put it anywhere and have it mean "the beginning of a line". C-hig (elisp)Regexp Special explains:

 For historical compatibility reasons, ‘^’ can be used
 only at the beginning of the regular expression, or
 after ‘\(’, ‘\(?:’ or ‘\|’.

More specifically, if you do use it in other positions then it matches a literal ^ character (but if you want to match a ^ then it's preferable -- for the sake of readability -- to escape it, even in these cases where you can get away without escaping it).

But note that you don't need a ^ to match the beginning of a line if the previous matched character was guaranteed to be a newline.

Your Answer

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

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