all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: Regexp replacement question
       [not found] <mailman.1436.1429782914.904.help-gnu-emacs@gnu.org>
@ 2015-04-23  9:52 ` Pascal J. Bourguignon
  2015-04-23 10:09 ` Joost Kremers
  1 sibling, 0 replies; 5+ messages in thread
From: Pascal J. Bourguignon @ 2015-04-23  9:52 UTC (permalink / raw)
  To: help-gnu-emacs

MBR <mbr@arlsoft.com> writes:

> I've been using Emacs regular expressions for ages, and I thought I
> had a pretty good command of them.  But I just found myself wanting to
> do something that seems like it should be fairly simple, and I can't
> figure out how to do it with regular expressions.  I'd like to be able
> to specify a repeat count in the replace string equal to the number of
> times a particular group in the search string matched.  For example,
> if I have a buffer containing one number per line, I can easily match
> all sequences of leading zeroes with the regexp:
>
>     ^0+
>
> I could identify that sequence of leading zeroes as group 1 with the regexp:
>
>     ^\(0+\)
>
> and then in the replacement string I can reference the string that
> group matched as \1.
>
> But what do I do if I want to replace leading zeroes with the same
> number of spaces.  E.G. if my file contains:
>
>     12345
>       123
>      7890
>         3
>
> I'd like to convert it to:
>
>     12345
>     __123
>     _7890
>     ____3
>
> (Actually, I'd like to convert leading zeroes to spaces, but I'm using
> underscore "_" instead of space to make it visible in this message.)
>
> To do that, I'd need to be able to specify a count in the replacement
> string.  Imagine there were a syntax applicable to replacement strings
> such that \{n\} meant repeat the previous character n times.  And
> similarly, imagine that \{\n\} meant repeat the previous character by
> however many times group n in the search string matched.  If I had
> that capability, I could search for:
>
>     ^\(0\)+
>
> and replace it with:
>
>     _\{\1\}
>
> I've read through the Emacs documentation, and I can't find anything
> that will allow me to convert leading zeroes to the same number of
> spaces, or vice versa.
>
> Ideas?
>
>    Mark Rosenthal
>    mbr@arlsoft.com <mailto:mbr@arlsoft.com>
>
>

M-x replace-regexp RET \<\(0+\) RET \,(make-string (length \1) 32) RET

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


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

* Regexp replacement question
@ 2015-04-23  9:54 MBR
  2015-04-23 15:05 ` Yuri Khan
  0 siblings, 1 reply; 5+ messages in thread
From: MBR @ 2015-04-23  9:54 UTC (permalink / raw)
  To: help-gnu-emacs

I've been using Emacs regular expressions for ages, and I thought I had 
a pretty good command of them.  But I just found myself wanting to do 
something that seems like it should be fairly simple, and I can't figure 
out how to do it with regular expressions.  I'd like to be able to 
specify a repeat count in the replace string equal to the number of 
times a particular group in the search string matched.  For example, if 
I have a buffer containing one number per line, I can easily match all 
sequences of leading zeroes with the regexp:

     ^0+

I could identify that sequence of leading zeroes as group 1 with the regexp:

     ^\(0+\)

and then in the replacement string I can reference the string that group 
matched as \1.

But what do I do if I want to replace leading zeroes with the same 
number of spaces.  E.G. if my file contains:

     12345
     00123
     07890
     00003

I'd like to convert it to:

     12345
     __123
     _7890
     ____3

(Actually, I'd like to convert leading zeroes to spaces, but I'm using 
underscore "_" instead of space to make it visible in this message.)

To do that, I'd need to be able to specify a count in the replacement 
string.  Imagine there were a syntax applicable to replacement strings 
such that \{n\} meant repeat the previous character n times.  And 
similarly, imagine that \{\n\} meant repeat the previous character by 
however many times group n in the search string matched.  If I had that 
capability, I could search for:

     ^\(0\)+

and replace it with:

     _\{\1\}

I've read through the Emacs documentation, and I can't find anything 
that will allow me to convert leading zeroes to the same number of 
spaces, or vice versa.

Ideas?

    Mark Rosenthal
    mbr@arlsoft.com <mailto:mbr@arlsoft.com>




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

* Re: Regexp replacement question
       [not found] <mailman.1436.1429782914.904.help-gnu-emacs@gnu.org>
  2015-04-23  9:52 ` Regexp replacement question Pascal J. Bourguignon
@ 2015-04-23 10:09 ` Joost Kremers
  2015-04-23 10:10   ` Joost Kremers
  1 sibling, 1 reply; 5+ messages in thread
From: Joost Kremers @ 2015-04-23 10:09 UTC (permalink / raw)
  To: help-gnu-emacs

EN:SiS(9)
MBR wrote:
> But what do I do if I want to replace leading zeroes with the same 
> number of spaces.  E.G. if my file contains:
[...]
> Ideas?

AFAIK "regular" regular expressions (pardon the pun) won't allow you to
do this. But at least with `replace-regexp' you can use Elisp
expressions in your replacement string, something like (untested):

M-x replace-regexp RET ^0+ RET
\,(make-string (length \&) " ")

Here, \& refers to the whole match. You can also use \1, \2, etc. to
refer to submatches.

IIUC such constructs aren't possible when the search/replace isn't
interactive, though.


HTH

-- 
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht


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

* Re: Regexp replacement question
  2015-04-23 10:09 ` Joost Kremers
@ 2015-04-23 10:10   ` Joost Kremers
  0 siblings, 0 replies; 5+ messages in thread
From: Joost Kremers @ 2015-04-23 10:10 UTC (permalink / raw)
  To: help-gnu-emacs

Joost Kremers wrote:
> \,(make-string (length \&) " ")

sorry, second arg to make-string should be an integer, so:

\,(make-string (length \&) 32)

-- 
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


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

* Re: Regexp replacement question
  2015-04-23  9:54 MBR
@ 2015-04-23 15:05 ` Yuri Khan
  0 siblings, 0 replies; 5+ messages in thread
From: Yuri Khan @ 2015-04-23 15:05 UTC (permalink / raw)
  To: MBR; +Cc: help-gnu-emacs@gnu.org

On Thu, Apr 23, 2015 at 3:54 PM, MBR <mbr@arlsoft.com> wrote:

> But what do I do if I want to replace leading zeroes with the same number of
> spaces.  E.G. if my file contains:
>
[…]
> Ideas?

Idea #1: You can repeatedly replace ^\(_*\)0 with \1_ until saturation.

Idea #2, if you deem that inelegant: You can use the “replace with
expression” form.

C-M-%
Query replace regexp: ^\(0+\)
Query replace regexp ^\(0+\) with: \,(make-string (length \1) ?_)



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

end of thread, other threads:[~2015-04-23 15:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1436.1429782914.904.help-gnu-emacs@gnu.org>
2015-04-23  9:52 ` Regexp replacement question Pascal J. Bourguignon
2015-04-23 10:09 ` Joost Kremers
2015-04-23 10:10   ` Joost Kremers
2015-04-23  9:54 MBR
2015-04-23 15:05 ` Yuri Khan

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.