help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to grep for a string spanning multiple lines?


From: Jean Louis
Subject: Re: How to grep for a string spanning multiple lines?
Date: Sat, 26 Nov 2022 11:43:47 +0300
User-agent: Mutt/2.2.7+37 (a90f69b) (2022-09-02)

* tomas@tuxteam.de <tomas@tuxteam.de> [2022-11-26 11:37]:
> Note that, at least, in Emacs, the POSIX character class [:space:]
> also matches line breaks. So if you always use [[:space:]]+ to
> separate your words, you might find what you are looking for.

Just that for some reason it does not work as expected:

(string-match "[[:space:]]+" "Hello\nthere") ➜ nil

(string-match "[[:space:]]+" "Hello
there") ➜ nil

(xr "[[:space:]]+") ➜ (one-or-more space)

(rx (one-or-more space)) ➜ "[[:space:]]+"

That is why I had to make this:

(defun rcd-string-clean-whitespace (s)
  "Return trimmed string S after cleaning whitespaces."
  (replace-regexp-in-string 
   (rx (one-or-more (or "\n" (any whitespace))))
   " "
   (string-trim s)))

as then it works as expected:

(rcd-string-clean-whitespace "Hello\nthere") ➜ "Hello there"

Because "[[:space:]]+" does not include "\n" that I know:

(replace-regexp-in-string "[[:space:]]+" " " "Hello\nthere") ➜ "Hello
there"



-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]