all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
@ 2012-02-18 17:52 Steve Petersen
  2012-02-18 19:17 ` Thorsten
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Steve Petersen @ 2012-02-18 17:52 UTC (permalink / raw)
  To: Help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 734 bytes --]

Hi emacs gurus,

[I accidentally posted this first to the main emacs forum, pardon my
newbness.]

I've spent hours scouring the web trying to solve what should be a simple
problem.  I appreciate any help!

I want to replace a three-word phrase with an acronym.  Should be easy,
right?  But of course I want to match across lines.  From what I read '\s-'
should match line feeds, but it doesn't.  The closest I've gotten to
matching across lines is using 'foo[\s-^J]+bar' (using ^Q to insert ^J
literally), but for some reason that *doesn't* match 'foo bar' on the same
line!  I'm out of ideas - what's going on?

I'm using GNU Emacs 23.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.35).

Thanks,
Steve

-- 

http://stevepetersen.net

[-- Attachment #2: Type: text/html, Size: 927 bytes --]

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

* Re: replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
  2012-02-18 17:52 replacing phrases: matching line feeds in regular expressions, since \s- doesn't work Steve Petersen
@ 2012-02-18 19:17 ` Thorsten
  2012-02-18 19:33 ` jeremiah.dodds
  2012-02-19  0:15 ` Eric Abrahamsen
  2 siblings, 0 replies; 9+ messages in thread
From: Thorsten @ 2012-02-18 19:17 UTC (permalink / raw)
  To: help-gnu-emacs

Steve Petersen <steve@stevepetersen.net> writes:

> Hi emacs gurus, 
>
> [I accidentally posted this first to the main emacs forum, pardon my
> newbness.]
>
> I've spent hours scouring the web trying to solve what should be a
> simple problem.  I appreciate any help!  
>
> I want to replace a three-word phrase with an acronym.  Should be
> easy, right?  But of course I want to match across lines.  From what I
> read '\s-' should match line feeds, but it doesn't.  The closest I've
> gotten to matching across lines is using 'foo[\s-^J]+bar' (using ^Q to
> insert ^J literally), but for some reason that doesn't match 'foo bar'
> on the same line!  I'm out of ideas - what's going on?

My uninformed and untested opinion is to just use M-x query-replace,
since you want to replace a string with another string. Maybe it doesn't
match when a line break is involved, but I would be surprised. 

Another possibility would be to use M-x regexp-builder, a tool that
helps you to visualize the matches for your regexp. 

cheers
--
Thorsten




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

* Re: replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
  2012-02-18 17:52 replacing phrases: matching line feeds in regular expressions, since \s- doesn't work Steve Petersen
  2012-02-18 19:17 ` Thorsten
@ 2012-02-18 19:33 ` jeremiah.dodds
  2012-02-18 20:44   ` Peter Dyballa
  2012-02-18 23:30   ` bitterspetey
  2012-02-19  0:15 ` Eric Abrahamsen
  2 siblings, 2 replies; 9+ messages in thread
From: jeremiah.dodds @ 2012-02-18 19:33 UTC (permalink / raw)
  To: help-gnu-emacs

Steve Petersen <steve@stevepetersen.net> writes:

> Hi emacs gurus, 
>
> [I accidentally posted this first to the main emacs forum, pardon my newbness.]
>
> I've spent hours scouring the web trying to solve what should be a simple
> problem.  I appreciate any help!  
>
> I want to replace a three-word phrase with an acronym.  Should be easy, right?
>  But of course I want to match across lines.  From what I read '\s-' should
> match line feeds, but it doesn't.  The closest I've gotten to matching across
> lines is using 'foo[\s-^J]+bar' (using ^Q to insert ^J literally), but for some
> reason that doesn't match 'foo bar' on the same line!  I'm out of ideas - what's
> going on?

The following works for me with the words separated by arbitrary
whitespace including newlines (although it is ugly, and I'm sure there's
better ways to do it):

M-x replace-regexp foo[ ^TAB^j]*bar[ ^TAB^j]*baz[ ^TAB^j] RET fbb RET

Where ^TAB and ^j are C-qTAB and C-qC-j, on 24.0.93.8. I don't know, but
I don't think that it should behave significantly differently on 23.




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

* Re: replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
  2012-02-18 19:33 ` jeremiah.dodds
@ 2012-02-18 20:44   ` Peter Dyballa
  2012-02-19  2:47     ` jeremiah.dodds
  2012-02-18 23:30   ` bitterspetey
  1 sibling, 1 reply; 9+ messages in thread
From: Peter Dyballa @ 2012-02-18 20:44 UTC (permalink / raw)
  To: jeremiah.dodds; +Cc: Steve Petersen, GNU Emacs List


Am 18.2.2012 um 20:33 schrieb jeremiah.dodds@gmail.com:

> M-x replace-regexp foo[ ^TAB^j]*bar[ ^TAB^j]*baz[ ^TAB^j] RET fbb RET

Exactly! A TAB has to be entered as C-q TAB and a line feed as C-q C-j. The textual descriptions of control characters are a bit too complicated. These are not the same in different versions of GNU Emacs while the hard ones are since eons.

--
Greetings

  Pete

Chicago, n.:
        Where the dead still vote … early and often!




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

* Re: replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
@ 2012-02-18 21:26 Silvio Levy
  2012-02-19  3:04 ` jeremiah.dodds
  0 siblings, 1 reply; 9+ messages in thread
From: Silvio Levy @ 2012-02-18 21:26 UTC (permalink / raw)
  To: Thorsten; +Cc: help-gnu-emacs


It seems to work for me with 'foo[\s^J]+bar' (no hyphen in the
character class -- the hyphen is for a range, like [a-z])

Silvio





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

* Re: replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
  2012-02-18 19:33 ` jeremiah.dodds
  2012-02-18 20:44   ` Peter Dyballa
@ 2012-02-18 23:30   ` bitterspetey
  1 sibling, 0 replies; 9+ messages in thread
From: bitterspetey @ 2012-02-18 23:30 UTC (permalink / raw)
  To: Help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 992 bytes --]


Thanks for the suggestions so far!


query-replace does not seem to work across newlines (to my surprise).


foo[\s^J]bar also does not work; I thought \s- was supposed to match any
whitespace, including newline, and not be interpreted as part of a range in
this case.


This one below does work, and thank you!  But as you say, it's kludgy. Is
this really the best emacs can do to replace a phrase?!  Why doesn't \s-
work in this context? Is there no way to match any whitespace?


Jeremiah Dodds wrote:
> 
> The following works for me with the words separated by arbitrary
> whitespace including newlines (although it is ugly, and I'm sure there's
> better ways to do it):
> 
> M-x replace-regexp foo[ ^TAB^j]*bar[ ^TAB^j]*baz[ ^TAB^j] RET fbb RET
> 
-- 
View this message in context: http://old.nabble.com/replacing-phrases%3A-matching-line-feeds-in-regular-expressions%2C-since-%5Cs--doesn%27t-work-tp33349186p33350095.html
Sent from the Emacs - Help mailing list archive at Nabble.com.

[-- Attachment #2: Type: text/html, Size: 1452 bytes --]

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

* Re: replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
  2012-02-18 17:52 replacing phrases: matching line feeds in regular expressions, since \s- doesn't work Steve Petersen
  2012-02-18 19:17 ` Thorsten
  2012-02-18 19:33 ` jeremiah.dodds
@ 2012-02-19  0:15 ` Eric Abrahamsen
  2 siblings, 0 replies; 9+ messages in thread
From: Eric Abrahamsen @ 2012-02-19  0:15 UTC (permalink / raw)
  To: help-gnu-emacs

On Sun, Feb 19 2012, Steve Petersen wrote:

> Hi emacs gurus,
>
> [I accidentally posted this first to the main emacs forum, pardon my
> newbness.]
>
> I've spent hours scouring the web trying to solve what should be a
> simple problem.  I appreciate any help! 
>
> I want to replace a three-word phrase with an acronym.  Should be
> easy, right?  But of course I want to match across lines.  From what
> I read '\s-' should match line feeds, but it doesn't.  The closest
> I've gotten to matching across lines is using 'foo[\s-^J]+bar' (using
> ^Q to insert ^J literally), but for some reason that doesn't match
> 'foo bar' on the same line!  I'm out of ideas - what's going on?

There are also char classes, which gives you [:space:], matching both
space and tab (it would be really nice to have one class that matches
space, tab and newline...). So you could do:

"foo[[:space:]^J]bar[[:space:]^J]baz[[:space:]^J]?"

That seems to work, though it's still ugly.

-- 
GNU Emacs 24.0.93.1 (i686-pc-linux-gnu, GTK+ Version 2.24.10)
 of 2012-02-16 on pellet




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

* Re: replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
  2012-02-18 20:44   ` Peter Dyballa
@ 2012-02-19  2:47     ` jeremiah.dodds
  0 siblings, 0 replies; 9+ messages in thread
From: jeremiah.dodds @ 2012-02-19  2:47 UTC (permalink / raw)
  To: Peter Dyballa; +Cc: Steve Petersen, GNU Emacs List


Peter Dyballa <Peter_Dyballa@Web.DE> writes:

> Am 18.2.2012 um 20:33 schrieb jeremiah.dodds@gmail.com:
>
>> M-x replace-regexp foo[ ^TAB^j]*bar[ ^TAB^j]*baz[ ^TAB^j] RET fbb RET
>
> Exactly! A TAB has to be entered as C-q TAB and a line feed as C-q C-j. The
> textual descriptions of control characters are a bit too complicated. These are
> not the same in different versions of GNU Emacs while the hard ones are since
> eons.

I can't *quite* tell if you're being sarcastic. The thing I wrote is
unnecessary, I'm sure -- I don't use emacs' regular expression support
for much of anything past fairly simple replacements these days,
preferring elisp functions and macros for editing tasks that I could do
with a huge regex.



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

* Re: replacing phrases: matching line feeds in regular expressions, since \s- doesn't work
  2012-02-18 21:26 Silvio Levy
@ 2012-02-19  3:04 ` jeremiah.dodds
  0 siblings, 0 replies; 9+ messages in thread
From: jeremiah.dodds @ 2012-02-19  3:04 UTC (permalink / raw)
  To: help-gnu-emacs

Silvio Levy <levy@msri.org> writes:

> It seems to work for me with 'foo[\s^J]+bar' (no hyphen in the
> character class -- the hyphen is for a range, like [a-z])
>
> Silvio

Ahh, right. '\s-' *does* match characters with a whitespace syntax, but
since it's in a character alternative the '-''s meaning as a
range-definer takes precedence.

As an aside, there are ... a lot of edge cases in the Special Characters
in Regular Expressions info node for elisp, even for docs for a regular
expression implementation. It might actually serve the purpose of
pushing people towards writing functions and using other methods for
non-trivial editing tasks.



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

end of thread, other threads:[~2012-02-19  3:04 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-18 17:52 replacing phrases: matching line feeds in regular expressions, since \s- doesn't work Steve Petersen
2012-02-18 19:17 ` Thorsten
2012-02-18 19:33 ` jeremiah.dodds
2012-02-18 20:44   ` Peter Dyballa
2012-02-19  2:47     ` jeremiah.dodds
2012-02-18 23:30   ` bitterspetey
2012-02-19  0:15 ` Eric Abrahamsen
  -- strict thread matches above, loose matches on Subject: below --
2012-02-18 21:26 Silvio Levy
2012-02-19  3:04 ` jeremiah.dodds

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.