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

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

Re: [SOLVED] Re: (rx regexp to remove space and new lines


From: Jean Louis
Subject: Re: [SOLVED] Re: (rx regexp to remove space and new lines
Date: Tue, 13 Sep 2022 07:01:22 +0300
User-agent: Mutt/+ () (2022-06-11)

* Michael Heerdegen <michael_heerdegen@web.de> [2022-09-13 05:45]:
> Jean Louis <bugs@gnu.support> writes:
> 
> >    (rx (one-or-more (or "\n" (any whitespace))))
> 
> It had been mentioned that newlines count as whitespace.  And
> (any CHARCLASS) can be simplified to CHARCLASS.  And you probably want
> greedy matching.  You get:
> 
>   (rx (+ whitespace))

That makes sense, but how?

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

(rcd-string-clean-whitespace "   hello   there  


and I    am her e") ⇒ "hello there and I am her e"

it works here in this buffer, but in other Emacs buffer it does not
work. This is maybe because whitespace is defined diffrently per
buffer? 

Anyway, in emacs -Q it does not work when I use:
   (rx (one-or-more (+ whitespace)))

Until I understand it, I have to stay with this version:

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

as it works in this buffer and in other buffers.

(rcd-string-clean-whitespace "   hello   there  


and I    am her e") ⇒ "hello there and I am her e"

-- 
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]