* string-replace question -- changing names like variable_name to variableName
@ 2002-09-03 13:17 bc
2002-09-03 14:24 ` John McCabe
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: bc @ 2002-09-03 13:17 UTC (permalink / raw)
Is there an easier way to do this short of some lisp like:
(replace-string "_a" "A")
(replace-string "_A" "A")
(replace-string "_b" "B")
etc.
Thanks,
bc
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: string-replace question -- changing names like variable_name to variableName
2002-09-03 13:17 string-replace question -- changing names like variable_name to variableName bc
@ 2002-09-03 14:24 ` John McCabe
2002-09-03 14:47 ` bc
2002-09-03 14:39 ` lawrence mitchell
2002-09-03 15:16 ` Stefan Monnier <foo@acm.com>
2 siblings, 1 reply; 6+ messages in thread
From: John McCabe @ 2002-09-03 14:24 UTC (permalink / raw)
On Tue, 03 Sep 2002 09:17:58 -0400, bc <bc@example.net> wrote:
>
>Is there an easier way to do this short of some lisp like:
>
>(replace-string "_a" "A")
>(replace-string "_A" "A")
>(replace-string "_b" "B")
yes.
From the documentation of replace-string...
"This function is usually the wrong thing to use in a Lisp program.
What you probably want is a loop like this:
(while (search-forward FROM-STRING nil t)
(replace-match TO-STRING nil t))
which will run faster and will not set the mark or print anything.
(You may need a more complex loop if FROM-STRING can match the null
string
and TO-STRING is also null.)"
Are you using a lisp program? If so, you probably want to use a
regexp-search and replace.
Try this function:
(defun get-rid-of-underscore ()
(interactive)
(while (re-search-forward "_\\([a-zA-Z]\\)?" nil t)
(replace-match (upcase (match-string 1)) nil nil)))
on this data:
hello_aorld
hello_Aorld
hello_borld
hello_Borld
hello_corld
hello_Corld
hello_dorld
hello_Dorld
hello_eorld
hello_Eorld
Does it work?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: string-replace question -- changing names like variable_name to variableName
2002-09-03 13:17 string-replace question -- changing names like variable_name to variableName bc
2002-09-03 14:24 ` John McCabe
@ 2002-09-03 14:39 ` lawrence mitchell
2002-09-03 15:16 ` Stefan Monnier <foo@acm.com>
2 siblings, 0 replies; 6+ messages in thread
From: lawrence mitchell @ 2002-09-03 14:39 UTC (permalink / raw)
bc wrote:
[...] variable_name --> variableName
> Is there an easier way to do this short of some lisp like:
> (replace-string "_a" "A")
> (replace-string "_A" "A")
> (replace-string "_b" "B")
> etc.
Why yes there is. Using a small amount of knowledge of regexps,
and a few other Emacs functions.
Note firstly, that the docstring of replace-string explicitly
recommends against its use in lisp programs:
/----[ C-h f replace-string RET ]
| This function is usually the wrong thing to use in a Lisp program.
| What you probably want is a loop like this:
| (while (search-forward FROM-STRING nil t)
| (replace-match TO-STRING nil t))
| which will run faster and will not set the mark or print anything.
\----
However, neither replace-string nor search-forward allow the use
of regexps in their syntax, hence, we need to use the regexp
equivalents, namely re-search-forward
Using these bits of information, we come up with
something like:
(let ((case-fold-search nil)) ; make sure we don't ignore
; case in our search, if this
; isn't a problem, then change
; the nil on this line to t.
(while (re-search-forward ; search forward for a regular
; expression.
"\\([a-z]\\)_\\([a-z]\\)" ; this regexp matches
; something of the form:
; foo_bar, but not _bar, or
; foo_. If you want to match
; capitalised letters add A-Z
; to the character
; groupings. Or change the
; binding of case-fold-search
; to t.
nil ; no bound on the search
t) ; don't error if the match is
; not found.
(replace-match ; replace the matched text with:
(concat (match-string 1) ; a concatenation of the 1st
; \\(..\\) grouping. and...
(upcase ; the uppercase equivalent of:
(match-string 2))) ; the 2nd \\(..\\) grouping.
t))) ; since we've gone to all the
; trouble of changing the
; case, treat our string as FIXEDCASE
I hope the comments are suitably explanatory.
--
lawrence mitchell <wence@gmx.li>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: string-replace question -- changing names like variable_name to variableName
2002-09-03 14:24 ` John McCabe
@ 2002-09-03 14:47 ` bc
2002-09-03 15:51 ` John McCabe
0 siblings, 1 reply; 6+ messages in thread
From: bc @ 2002-09-03 14:47 UTC (permalink / raw)
John McCabe wrote:
>
<snip>
> Try this function:
>
> (defun get-rid-of-underscore ()
> (interactive)
> (while (re-search-forward "_\\([a-zA-Z]\\)?" nil t)
> (replace-match (upcase (match-string 1)) nil nil)))
>
> on this data:
>
> hello_aorld
> hello_Aorld
> hello_borld
> hello_Borld
> hello_corld
> hello_Corld
> hello_dorld
> hello_Dorld
> hello_eorld
> hello_Eorld
>
> Does it work?
Works great, and very educational on helping with
my lisp programming. Thanks much!
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: string-replace question -- changing names like variable_name to variableName
2002-09-03 13:17 string-replace question -- changing names like variable_name to variableName bc
2002-09-03 14:24 ` John McCabe
2002-09-03 14:39 ` lawrence mitchell
@ 2002-09-03 15:16 ` Stefan Monnier <foo@acm.com>
2 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2002-09-03 15:16 UTC (permalink / raw)
Continuing the great tradition of answering to a different question than
the one that was asked, I'll point you to glasses-mode (distributed with
Emacs-21) which doesn't change the buffer's content but allows you to
display "variableName" as "variable_name".
Stefan
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: string-replace question -- changing names like variable_name to variableName
2002-09-03 14:47 ` bc
@ 2002-09-03 15:51 ` John McCabe
0 siblings, 0 replies; 6+ messages in thread
From: John McCabe @ 2002-09-03 15:51 UTC (permalink / raw)
On Tue, 03 Sep 2002 10:47:40 -0400, bc <bc@example.net> wrote:
>> Does it work?
>
>Works great, and very educational on helping with
>my lisp programming. Thanks much!
You're welcome. Glad to be of assistance.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2002-09-03 15:51 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-09-03 13:17 string-replace question -- changing names like variable_name to variableName bc
2002-09-03 14:24 ` John McCabe
2002-09-03 14:47 ` bc
2002-09-03 15:51 ` John McCabe
2002-09-03 14:39 ` lawrence mitchell
2002-09-03 15:16 ` Stefan Monnier <foo@acm.com>
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).