all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* I can't believe: replace regexp in a string
@ 2003-02-14  8:37 Stefan Kamphausen
  2003-02-14  9:04 ` Klaus Berndl
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Stefan Kamphausen @ 2003-02-14  8:37 UTC (permalink / raw)


Dear elispWizards,

somehow I can't believe it. When I try to replace a regexp in a string
I have to create a temporary buffer and use the usual buffer
replacement commands or write a loop over the sequence. Is that true?
I have found a posting in this group describing that and have looked
at the XEmacs sources of subr.el where a defun replace-in-string is
defined which does one of the above procedures depending on the length
of the string. Having coded in Perl a good deal this seems quite
strange to me, but if it is the elispish way to do it, I'll give in
:-)

Best Regars and Thanks
Stefan Kamphausen

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: I can't believe: replace regexp in a string
  2003-02-14  8:37 I can't believe: replace regexp in a string Stefan Kamphausen
@ 2003-02-14  9:04 ` Klaus Berndl
  2003-02-14  9:11 ` David Kastrup
  2003-02-14 15:55 ` Stefan Monnier <foo@acm.com>
  2 siblings, 0 replies; 7+ messages in thread
From: Klaus Berndl @ 2003-02-14  9:04 UTC (permalink / raw)


On 14 Feb 2003, Stefan Kamphausen wrote:

>  Dear elispWizards,
>  
>  somehow I can't believe it. When I try to replace a regexp in a string
>  I have to create a temporary buffer and use the usual buffer
>  replacement commands or write a loop over the sequence. Is that true?
>  I have found a posting in this group describing that and have looked
>  at the XEmacs sources of subr.el where a defun replace-in-string is
>  defined which does one of the above procedures depending on the length
>  of the string. Having coded in Perl a good deal this seems quite
>  strange to me, but if it is the elispish way to do it, I'll give in
> :-)

There is no need to believe this because elisp can also replace regexps in
strings - even without temp. buffers.

In general a combination of string-match and replace-match does the trick, see
the doc-strings; both of them can get a STRING argument.

For replacing all occurences of a regexp in a string a while loop with
string-match (the condition) and replace-match (the body) is sufficient.

I have already written a function:

(defun re-replace-all-string (regexp replacement string
                                     &optional regreg start end fixedcase
                                     literal subexp)
"Replaces in STRING REGEXP with REPLACEMENT between START and END
\(or - if not nil - within the substring specified by REGREG) and returns
a list containing:
1) the new string
2) the start (usefull if substring is defined by REGREG)
3) the - possible - new END
4) the number of replacements made.
Returns nil if STRING is nil.
For START and END nil means begin of string (end of string).
For the arguments REPLACEMENT, FIXEDCASE, LITERAL and SUBEXP look
at `replace-match' with one extension: If REPLACEMENT is exactly
`<any single char>*' then the matching is overwritten with a string of
same length consisting of <any single char>. If REPLACEMENT is exactly
`<any single char>\\*' then the REPLACEMANT will be interpreted as really
'`<any single char>*', i.e. a character followed by a star. This extension
is only active if LITERAL is nil!"
 ;; here comes the code
 )

If you want i can post it...

Ciao,
Klaus

-- 
Klaus Berndl			mailto: klaus.berndl@sdm.de
sd&m AG				http://www.sdm.de
software design & management	
Thomas-Dehler-Str. 27, 81737 München, Germany
Tel +49 89 63812-392, Fax -220

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: I can't believe: replace regexp in a string
  2003-02-14  8:37 I can't believe: replace regexp in a string Stefan Kamphausen
  2003-02-14  9:04 ` Klaus Berndl
@ 2003-02-14  9:11 ` David Kastrup
  2003-02-14 15:55 ` Stefan Monnier <foo@acm.com>
  2 siblings, 0 replies; 7+ messages in thread
From: David Kastrup @ 2003-02-14  9:11 UTC (permalink / raw)


kamphausen@creativepharma.com (Stefan Kamphausen) writes:

> Dear elispWizards,
> 
> somehow I can't believe it. When I try to replace a regexp in a string
> I have to create a temporary buffer and use the usual buffer
> replacement commands or write a loop over the sequence. Is that
> true?

No.

(let (pos)
  (while (setq pos (string-match "???" string ... pos))
    (setq string (replace-match ...)))

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: I can't believe: replace regexp in a string
  2003-02-14  8:37 I can't believe: replace regexp in a string Stefan Kamphausen
  2003-02-14  9:04 ` Klaus Berndl
  2003-02-14  9:11 ` David Kastrup
@ 2003-02-14 15:55 ` Stefan Monnier <foo@acm.com>
  2003-02-15 15:07   ` Stefan Kamphausen
  2 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-02-14 15:55 UTC (permalink / raw)


> somehow I can't believe it. When I try to replace a regexp in a string
> I have to create a temporary buffer and use the usual buffer
> replacement commands or write a loop over the sequence. Is that true?
> I have found a posting in this group describing that and have looked
> at the XEmacs sources of subr.el where a defun replace-in-string is
> defined which does one of the above procedures depending on the length
> of the string. Having coded in Perl a good deal this seems quite
> strange to me, but if it is the elispish way to do it, I'll give in

I'm not sure what's your complaint exactly.
Is it that XEmacs' replace-in-string is not standard in Emacs (this
is addressed in Emacs-21 with replace-regexp-in-string) or is it
that you find both implementations of replace-in-string inefficient
(how is it implemented in Perl) ?

Finally, the reason why it took so long for Emacs to provide
replace-regexp-in-string is because Emacs uses buffers a lot more than
strings, so if you need replace-regexp-in-string it's maybe because you
made the arguably wrong decision to use a string rather than a buffer.

Perl is great with strings, Emacs is great with buffers.


        Stefan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: I can't believe: replace regexp in a string
  2003-02-14 15:55 ` Stefan Monnier <foo@acm.com>
@ 2003-02-15 15:07   ` Stefan Kamphausen
  2003-02-15 17:48     ` Kai Großjohann
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Kamphausen @ 2003-02-15 15:07 UTC (permalink / raw)


Dear Stefan

"Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> wrote in message news:<5lheb6zwkj.fsf@rum.cs.yale.edu>...
> > somehow I can't believe it. When I try to replace a regexp in a string
> > I have to create a temporary buffer and use the usual buffer
> > replacement commands or write a loop over the sequence. Is that true?
> > I have found a posting in this group describing that and have looked
> > at the XEmacs sources of subr.el where a defun replace-in-string is
> > defined which does one of the above procedures depending on the length
> > of the string. Having coded in Perl a good deal this seems quite
> > strange to me, but if it is the elispish way to do it, I'll give in
> 
> I'm not sure what's your complaint exactly.
> Is it that XEmacs' replace-in-string is not standard in Emacs (this
> is addressed in Emacs-21 with replace-regexp-in-string) or is it
> that you find both implementations of replace-in-string inefficient
> (how is it implemented in Perl) ?

I'm just wondering that such a trivial task (if your used to "$string
=~ s/match-re/replace-re/" in Perl) like doing a match and replace on
a string seems to be really non-trivial in Emacs, and I'm speaking of
both Emacsen here. I thought I must have missed something somewhere.
 
> Finally, the reason why it took so long for Emacs to provide
> replace-regexp-in-string is because Emacs uses buffers a lot more than
> strings, so if you need replace-regexp-in-string it's maybe because you
> made the arguably wrong decision to use a string rather than a buffer.

Hm, I'm parsing a text file and need to replace some trailing
whitespaces from a substring I just read. You're right in that I could
do that replacement in the buffer I use for reading the files'
contents (and thinking about that right now I might just do it that
way ;-). Thanks for that hint.

> Perl is great with strings, Emacs is great with buffers.

:-)

And thanks to all the other posters! I will be able to find a solution
with all those hints :-)

Thanks and Regards
Stefan Kamphausen

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: I can't believe: replace regexp in a string
  2003-02-15 15:07   ` Stefan Kamphausen
@ 2003-02-15 17:48     ` Kai Großjohann
  2003-02-17 11:24       ` Stefan Kamphausen
  0 siblings, 1 reply; 7+ messages in thread
From: Kai Großjohann @ 2003-02-15 17:48 UTC (permalink / raw)


kamphausen@creativepharma.com (Stefan Kamphausen) writes:

> "Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> wrote in message news:<5lheb6zwkj.fsf@rum.cs.yale.edu>...
>> 
>> I'm not sure what's your complaint exactly.
>> Is it that XEmacs' replace-in-string is not standard in Emacs (this
>> is addressed in Emacs-21 with replace-regexp-in-string) or is it
>> that you find both implementations of replace-in-string inefficient
>> (how is it implemented in Perl) ?
>
> I'm just wondering that such a trivial task (if your used to "$string
> =~ s/match-re/replace-re/" in Perl) like doing a match and replace on
> a string seems to be really non-trivial in Emacs, and I'm speaking of
> both Emacsen here. I thought I must have missed something somewhere.

Well, operations in a buffer are simpler, perhaps.

>> Finally, the reason why it took so long for Emacs to provide
>> replace-regexp-in-string is because Emacs uses buffers a lot more than
>> strings, so if you need replace-regexp-in-string it's maybe because you
>> made the arguably wrong decision to use a string rather than a buffer.
>
> Hm, I'm parsing a text file and need to replace some trailing
> whitespaces from a substring I just read. You're right in that I could
> do that replacement in the buffer I use for reading the files'
> contents (and thinking about that right now I might just do it that
> way ;-). Thanks for that hint.

Why does the substring contain the spaces in the first place?

Generally, when parsing a text file it is often better to use general
movement functions rather than to rely on regular expressions.
(Sometimes, regexes are the right tool even in Emacs.  But in Perl, a
regex is ALWAYS the right solution, in Emacs it SELDOM is.)

For example, you could frob the syntax correctly and then what you're
looking for might be a word, or a string, or an s-expression.

In your specific case, if you use searching in the buffer to find the
right spot, then you can use skip-syntax-backward or
skip-chars-backward to skip backwards over the trailing spaces.
Problem solved :-)

-- 
A turnip curses Elvis

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: I can't believe: replace regexp in a string
  2003-02-15 17:48     ` Kai Großjohann
@ 2003-02-17 11:24       ` Stefan Kamphausen
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Kamphausen @ 2003-02-17 11:24 UTC (permalink / raw)


Hi,

kai.grossjohann@uni-duisburg.de (Kai Großjohann wrote in message news:<848ywhv3jg.fsf@lucy.is.informatik.uni-duisburg.de>...
> Generally, when parsing a text file it is often better to use general
> movement functions rather than to rely on regular expressions.
> (Sometimes, regexes are the right tool even in Emacs.  But in Perl, a
> regex is ALWAYS the right solution, in Emacs it SELDOM is.)
> 
> For example, you could frob the syntax correctly and then what you're
> looking for might be a word, or a string, or an s-expression.
> 
> In your specific case, if you use searching in the buffer to find the
> right spot, then you can use skip-syntax-backward or
> skip-chars-backward to skip backwards over the trailing spaces.
> Problem solved :-)

Yes, I think I needed someone to point me to the fact that an lispish
approach is probably different to a perlish one :-)
Actually yesterday in the evening I managed to rewrite that parser and
it now works in both Emacsen and doesn't need the replace-in-string
anymore :-)
More than that, the code is much cleaner now.

Thanks and Regards
stefan kamphausen

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-02-17 11:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-14  8:37 I can't believe: replace regexp in a string Stefan Kamphausen
2003-02-14  9:04 ` Klaus Berndl
2003-02-14  9:11 ` David Kastrup
2003-02-14 15:55 ` Stefan Monnier <foo@acm.com>
2003-02-15 15:07   ` Stefan Kamphausen
2003-02-15 17:48     ` Kai Großjohann
2003-02-17 11:24       ` Stefan Kamphausen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.