strange replace-regexp behavior

strange replace-regexp behavior

Post by patrick m. ry » Tue, 29 Dec 1992 22:39:23



        I was trying to remove a leading column of '>' characters
from some text and got some strange behavior from replace-regexp.
I have text something like this:


>>blah blah blah
>> blah blah blah

I wanted to remove the first '>' in each line.  Thus, I did:

        (replace-regexp "^>" "")

Instead of removing just the first '>' in each line, it removed
the whole sequence of '>' characters.  It acted as though I
has used "^>*" for the first argument.  I ended up with:


blah blah blah
 blah blah blah

I'm using Epoch 4.2 on a SPARC 10 under SunOS 4.1.3.

Any help would be appreciated.

thanks,
pat

--
"I have a cunning plan." -- Baldric
                                                             patrick m. ryan
     nasa / goddard space flight center / oceans and ice branch / hughes stx

 
 
 

strange replace-regexp behavior

Post by Lemming Productions Present. » Wed, 30 Dec 1992 02:28:34



>>>>> said:

Patrick>     I was trying to remove a leading column of '>' characters
Patrick> from some text and got some strange behavior from replace-regexp.
Patrick> I have text something like this:


>>blah blah blah
>> blah blah blah

Patrick> I wanted to remove the first '>' in each line.  Thus, I did:

Patrick>     (replace-regexp "^>" "")

Patrick> Instead of removing just the first '>' in each line, it removed
Patrick> the whole sequence of '>' characters.  It acted as though I
Patrick> has used "^>*" for the first argument.  I ended up with:

Ok, here's the deal. What emacs does is to do a re-search-forward for your
search string, deletes it, and inserts the replacement string. There goes
the "^>". Now, it repeats the re-search-forward. Lo and behold, point is
sitting just to the left of the search string "^>". It gleefully replaces
it with the replacement string, and repeats itself.

You may want to use querey-replace-regexp instead. Or do two replace-regexp
commands, the first change "^>" into something else, like "><", then do a
second replace-regexp on the new string.

--Rat                               PGP Public Key Block available upon request
||||| | | | |  |  |  |   |   |    |    |    |   |   |  |  |  |  | | | | | |||||

Love is a snowmobile racing across the tundra and then suddenly it flips over,
pinning you underneath. At night, the ice weasels come.
                                                 --Nietzsche [or Matt Groening]

 
 
 

strange replace-regexp behavior

Post by Richard Levit » Wed, 30 Dec 1992 03:15:27


Hmm... I tried to reply by mail, but it bounced.

[...]


>>blah blah blah
>> blah blah blah

pmr> I wanted to remove the first '>' in each line.  Thus, I did:

pmr>    (replace-regexp "^>" "")

[...]

=>

pmr> blah blah blah
pmr>  blah blah blah

I think this is what is happening (^ is the point after each operation):


         ^

        ^
...
Match:  >>blah blah blah
         ^
Repl:   >blah blah blah
        ^

Match:  >blah blah blah
         ^
Repl:   blah blah blah
        ^

... and the same for the last line.

Instead, try:

        (replace-regexp "^>\\(.*$\\)" "\\1")

--
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
! Richard Levitte, VMS GNU Emacs hacker   ! tel: int+46-8-18 30 99            !
! Sulv"agen 57, II                        ! fax: none for the moment          !

! SWEDEN                                  !                                   !
!-----------------------------------------------------------------------------!

 
 
 

strange replace-regexp behavior

Post by Rent-a-Lemmi » Wed, 30 Dec 1992 02:32:07


>>>>> On Mon, 28 Dec 1992 17:28:34 GMT, Lemming Productions Present...


Ratinox> You may want to use querey-replace-regexp instead. Or do two
Ratinox> replace-regexp commands, the first change "^>" into something
Ratinox> else, like "><", then do a second replace-regexp on the new
Ratinox> string.

I just realized a simpler way to do this. Define a keyboard macro that does
a "(re-search-forward "^>") C-d C-n". Then use a big prefix count before
C-x C-e to execute it.

--Rat                               PGP Public Key Block available upon request
||||| | | | |  |  |  |   |   |    |    |    |   |   |  |  |  |  | | | | | |||||

Carpe Joltem! (Seize the Caffeine)                                         --Me

 
 
 

strange replace-regexp behavior

Post by Ken Olst » Wed, 30 Dec 1992 04:22:20


Rat> I just realized a simpler way to do this. Define a keyboard macro
Rat> that does a "(re-search-forward "^>") C-d C-n". Then use a big
Rat> prefix count before C-x C-e to execute it.

Give it a prefix arg of 0.

 
 
 

strange replace-regexp behavior

Post by Mike Patt » Thu, 31 Dec 1992 23:45:38


I've seen a couple of good descriptions of what went wrong and some
interesting descriptions of how to fix this, but nobody has yet
suggested the one that immediately came to me.  Don't use
replace-regexp at all...  Here's what I'd use to delete the first and
only the first ">" from a set of lines

        (replace-string "\n>" "\n")

I just tried it on your sample text:


Quote:>blah blah blah
> blah blah blah

 
 
 

strange replace-regexp behavior

Post by Rob Sarge » Mon, 04 Jan 1993 00:30:44


Perhaps the most direct and generic way to remove the first column
from consecutive lines of text is kill-rectangle.
rjs

--
Rob Sargent                      s-mail: Dept. of Human Genetics    

                                         Eccles Genetics Bldg
                                         Salt Lake City, Utah  84112

 
 
 

1. Strange behavior with search and replace under Viper

Hello All,

under Viper, I can do standard search and replace operations the VI way
without any problem (%s/bli bla/BLA BLI/) except when you want to
replace by the word SET followed by a space: %s/bli bla/SET / : as soon
as you type the space, the command gets executed and it is not possible
to add anything there after. I am using Xemacs 21.1.8.

Any ideas?

Regards,
Tom

2. Sharing file handles amongst DLL calls from NTVDM

3. Strange behaviour of replace-regular-expression (?)

4. A2 ADDS

5. replace-regexpt/query-replace-regexp across ringbuffer?

6. Portable Electronics in Flights

7. Replace-region & replace-region-regexp?

8. Extract Files

9. replace-regexp on region ?

10. Delete empty lines of an Windows File with replace-regexp

11. replace-regexp

12. replace-regexp?

13. replace-regexp and case