Joe
Joe

Reputation: 1564

Emacs Lisp Regular Expression Match everything until character sequence

I am trying to write a regular expression in emacs lisp that will match multi line comments.

For example:

{-
  Some

  Comment

  Here
-}

Should match as a comment. Basically, anything between {- and -}. I am able to almost do it by doing the following:

"\{\-[^-]*\-\}"

However, this will fail if the comment includes a - not immediately followed by }

So, it will not match correctly in this case:

{-
  Some -
  Comment -
  Here -
-}

Which should be valid.

Basically, I would like to match on everything (including newlines) up to the sequence -}

Thanks in advance!

Upvotes: 3

Views: 984

Answers (2)

Drew
Drew

Reputation: 30708

Doesn't this work for you? {-[^-]*[^}]*-}

(You didn't specify things precisely, so I'm just guessing what you want. Must the {- and -} be at the line beginning? Must they be on lines by themselves? Must there be some other characters between them? Etc. For example, should it match a line like this? {--}?)

Upvotes: 1

Andreas Röhler
Andreas Röhler

Reputation: 4804

Made a toolkit for such cases. It comes with a parser, beg-end.el.

Remains to write a function, which will determine the beginning resp. the end of the object.

In pseudo-code:

(put 'MY-FORM 'beginning-op-at
           (lambda () (search-forward "-}")))

(put 'MY-FORM 'end-op-at
     (lambda () (search-backward "{-")))

When done, it's should be available, i.e. copied and returned like this

(defun MY-FORM-atpt (&optional arg)
  " "
  (interactive "p")
  (ar-th 'MY-FORM arg))

Get it here:

https://launchpad.net/s-x-emacs-werkstatt/

Upvotes: 1

Related Questions