2

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 ?

1

1 Answer 1

4

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.

4
  • Thank you explaining rx macro.
    – maindoor
    Commented Aug 11, 2019 at 23:50
  • @Drew Thanks for editing!
    – clemera
    Commented Aug 12, 2019 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
    Commented Aug 12, 2019 at 13:13
  • @npostavs Thanks, nice to know!
    – clemera
    Commented Aug 12, 2019 at 13:19

Your Answer

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

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