Cannot make cl-ppcre work with mulit-line mode

60 views Asked by At

I have two versions of the word hi.

(defvar x "```hi```")

(defvar y "```
             hi
           ```")

I also have a regex scanner:

(defvar scanner (cl-ppcre:create-scanner "```(.*?)```" :multi-line-mode t))

When I regex-replace on x, it returns as it should. but the multi-line-mode doesn't seem to work.

(cl-ppcre:regex-replace scanner x "\\1") ;; => "hi"

(cl-ppcre:regex-replace scanner y "\\1") ;; => "```
                                                hi
                                                ```"

I want everything between "```" to be replaced, irrespective of how many lines the string is.

What am I doing wrong?

1

There are 1 answers

2
ignis volens On BEST ANSWER

You need (create-scanner ... :multi-line-mode t :single-line-mode t)

Yes, this is insane. But it's equivalent to what Perl does: in particular, from perlre:

. Match any character (except newline)

and the /s modifier:

Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.