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

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

Re: Negative Lookahead Equivalent in emacs


From: Felix Dietrich
Subject: Re: Negative Lookahead Equivalent in emacs
Date: Tue, 09 May 2017 12:03:27 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

luishenriquezperez@gmail.com writes:

> I'm trying to write a regex that matches the last character of a
> sequence of non-whitespace characters '[^\n\r\t\f ]', or an empty line
> matching ^$.
>
> Thus: 
> Hello World! --> "o" and "!" would be matched
>
> In non-elisp regex languages I know the code for this is: \S(?!\S) 
> I know that \S is equivalent too [^ /n/r/t/f].

For your described behaviour something like the following might come
close to what you had in mind and does not require negative lookahead
support:

    \\(?:[^\n\r\t\f ]*\\([^\n\r\t\f ]\\)\\)\\|^\n

The first shy group "\(?:\)" matches zero or more non-whitespace
characters and one more non-whitespace character (everything following
must be a whitespace character); the one non-whitespace character will
be available as the first group of the resultant match.  Alternatively
"\|" it matches a line beginning with and (depending possibly on the
newline convention) therefore containing only a newline character,
i.e. an empty line.

Don't get confused by the plethora of backslashes: they require escaping
in an Emacs Lisp string to reach the regular expression functions as
proper backslashes; if they were not escaped they themself would escape
the following character.

> But I'm unsure of what the elisp equivalent (if any) of the negative
> lookahead (?!).

I do not know of the existence of an equivalent for the negative
lookahead feature of other regular expression engines in Emacs Lisp
regular expressions.

--
Felix Dietrich


reply via email to

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