I have the following line :

mod_name func_name /users/username/workspace/path_name/file.c 283

I wanted to capture the 3rd and 4th entry in this line, I am able to do that using regular expressions like so: ^(?:\S+\s){2}(\S+)\ ([0-9]+)

How do I translate this into a lisp regex ? so that elisp functions can understand. I couldn't use the above regex into re-builder within emacs. Says it is an invalid expression. What am I doing wrong ?


Emacs regexps do not use \S or \s for non-whitespace and whitepsace. See (info "(elisp) Syntax of Regexps") for details. You can evaluate that which will jump to the corresponding info page.

It's far easier and more readable to use the rx macro to create regexps in Emacs. Here is my translation to rx syntax of your example:

(rx line-start
    (= 2 (and (one-or-more any) " "))
    (group (one-or-more any))
    " "
    (group (one-or-more digit)))

And here is resulting regex string:

"^\\(?:.+ \\)\\{2\\}\\(.+\\) \\([0-9]+\\)"

Note that any which is expressed in regexps as "." does not match newlines in Emacs.

| improve this answer | |
  • Thank you explaining rx macro. – maindoor Aug 11 '19 at 23:50
  • @Drew Thanks for editing! – clemera Aug 12 '19 at 5:10
  • 1
    '**any** which is expressed in regexps as "." does not match newlines in Emacs.' - you can also write it as not-newline or nonl to make that clearer. – npostavs Aug 12 '19 at 13:13
  • @npostavs Thanks, nice to know! – clemera Aug 12 '19 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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