To match with multiple line regexp, I can use \\(.*\\|\n\\)
, but I think it should be [.*\\|\n\\]
which actually doesn't work as expected.
#+NAME: exam1
#+BEGIN_SRC elisp :results output
(let ((str "")
(start 0))
(setq str "this is\na test and this is a test")
(while (string-match "this is\\(.*\\|\n\\)a test" str start)
(setq start (match-end 0))
(print (match-string 0 str))))
#+END_SRC
#+RESULTS:
:
: "this is
: a test"
:
: "this is a test"
#+NAME: exam2
#+BEGIN_SRC elisp :results output
(let ((str "")
(start 0))
(setq str "this is\na test and this is a test")
(while (string-match "this is[.*\\|\n]a test" str start)
(setq start (match-end 0))
(print (match-string 0 str))))
#+END_SRC
#+RESULTS: exam2
:
: "this is
: a test"
What's the reason exam1 works but exam2 doesn't?
[.*\\|\n\\]
matches a single character - any of these characters:.*\|
(period, asterisk, backslash, vertical bar) or a newline character.