help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regexp newline issue


From: Kevin Rodgers
Subject: Re: regexp newline issue
Date: Tue, 29 Nov 2005 11:23:48 -0700
User-agent: Mozilla Thunderbird 0.9 (X11/20041105)

Christopher C. Stacy wrote:
> I want to search for a string like "foo bar",
> but there might be whitespace (even newlines)
> between any of the characters.
>
> Given the string "foo bar", I tried constructing a regexp by
>
> (replace-regexp-in-string "\\(.\\)"
>                             "[\040\011\012\015]*\\1"
>                             "foo bar")
>
> but that doesn't quite do it.
>
> By the way, I originally tried whitespace syntax "\\s-"
> rather than character alternatives, but that seemed to
> work even less well.  Also, I am using an older emacs
> that does not seem to have the named character classes.

The whitespace syntax class depends on the current syntax table, which
is specific to the buffer's major mode (and can even be overridden by
text properties in the buffer).

BTW, I think this is more readable than the octal notation: "[ \t\n\r]"

> In particular, the above [tab,LF,CR] hack finds #1 and #3,
> but not #2, in the following examples:
>
> ----------
>
> 1. foo bar
>
> 2. foo
> bar
>
> -or-
>
> 3. fo
> o ba
> r
> ----------

That's because the regexp you generate requires each of the characters
in the string to be present, including the space between the second "o"
and "b".  As John Paul Wellington showed, you should ignore the
whitespace characters in the string when generating the regexp.

--
Kevin Rodgers





reply via email to

[Prev in Thread] Current Thread [Next in Thread]