3

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!

4
  • 3
    See if this works for you: {-[[:unibyte:]]+?-} Commented Dec 21, 2013 at 3:27
  • If you submit that as a solution, I will award it to you. Otherwise, I will add it myself. Commented Dec 21, 2013 at 5:03
  • On additional consideration, this solution still does not work for some more complex comments. Commented Dec 21, 2013 at 13:32
  • If you still haven't resolved it, update you post with an example of what it didn't match. Commented Dec 22, 2013 at 0:45

2 Answers 2

1

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? {--}?)

Sign up to request clarification or add additional context in comments.

Comments

1

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/

2 Comments

Does this return a regular expression? I am trying to use this with font-lock highlighting so it must be in the form of a regular expression.
@Joe Example shown returns the string matched. Emacs-Werkstatt delivers related functions, which deliver beg-end positions etc. When searching a form defined by beginning and end, which spans an arbitrary number of lines, reg-exps can't match that reliably. A parser is needed. MY-FORM-atpt would call the parser on MY-FORM, reading the definitions. BTW the real case would ask if already at beginning, i.e. looking at "{-" - the lambda must be extended still. Also you will ask if it should match inside a string. Basically it should be useable to fontify any section found.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.