all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Negative Lookahead Equivalent in emacs
@ 2017-05-09  5:15 luishenriquezperez
  2017-05-09  7:16 ` Emanuel Berg
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: luishenriquezperez @ 2017-05-09  5:15 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

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]. 
But I'm unsure of what the elisp equivalent (if any) of the negative lookahead (?!).

I saw on this forum a post  "gnu.emacs.help › regex nirvana - near miss"
Where Drew Adams said: "Typically, what you want to do for this in Emacs Lisp is to combine 
the use of a regexp for positive matching with other code that takes 
care of the non-matching (negation) need. "

However, I'm not sure how to go about doing this.


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

* Re: Negative Lookahead Equivalent in emacs
  2017-05-09  5:15 Negative Lookahead Equivalent in emacs luishenriquezperez
@ 2017-05-09  7:16 ` Emanuel Berg
  2017-05-09  8:44 ` hector
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2017-05-09  7:16 UTC (permalink / raw)
  To: help-gnu-emacs

> 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

I would probably use syntax classes [1] for
this, with numbered groups, as in this Elisp.
Regexps are hairy tho so some fiddling and
fine-tuning is expected:

    (when (re-search-forward
           "\\([[:graph:]]\\)[ \n]" (point-max) t)
      (goto-char (match-beginning 0))
      (message (match-string-no-properties 1)) )

Hello World!

[1] https://www.emacswiki.org/emacs/RegularExpression

-- 
underground experts united
http://user.it.uu.se/~embe8573




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

* Re: Negative Lookahead Equivalent in emacs
  2017-05-09  5:15 Negative Lookahead Equivalent in emacs luishenriquezperez
  2017-05-09  7:16 ` Emanuel Berg
@ 2017-05-09  8:44 ` hector
  2017-05-09  9:54 ` Wasell
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: hector @ 2017-05-09  8:44 UTC (permalink / raw)
  To: help-gnu-emacs

On Mon, May 08, 2017 at 10:15:42PM -0700, luishenriquezperez@gmail.com wrote:
> Hi,
> 
> 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]. 
> But I'm unsure of what the elisp equivalent (if any) of the negative lookahead (?!).
> 
> I saw on this forum a post  "gnu.emacs.help › regex nirvana - near miss"
> Where Drew Adams said: "Typically, what you want to do for this in Emacs Lisp is to combine 
> the use of a regexp for positive matching with other code that takes 
> care of the non-matching (negation) need. "
> 
> However, I'm not sure how to go about doing this.

Perhaps

\\S-\\s-

or

[^[:space:]][[:space:]]

not including the ^$. I guess you just had to add "\\|^$" to the preceding expressions.
I'm not sure if there is any difference between the two syntaxes.

Note that these expressions depend on the syntax table. So the definition of
"[[:space:]]" can change depending on syntax table. It is not necessarily
"[\n\t\f\r\v ]"



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

* Re: Negative Lookahead Equivalent in emacs
  2017-05-09  5:15 Negative Lookahead Equivalent in emacs luishenriquezperez
  2017-05-09  7:16 ` Emanuel Berg
  2017-05-09  8:44 ` hector
@ 2017-05-09  9:54 ` Wasell
  2017-05-10 14:23   ` luishenriquezperez
  2017-05-09 10:03 ` Felix Dietrich
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Wasell @ 2017-05-09  9:54 UTC (permalink / raw)
  To: help-gnu-emacs

On Mon, 8 May 2017 22:15:42 -0700 (PDT), in article <49e1dd7d-4be5-4b03-b9e2-
e26b15b0a6cb@googlegroups.com>, luishenriquezperez@gmail.com wrote:
> 
> Hi,
> 
> 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]. 
> But I'm unsure of what the elisp equivalent (if any) of the negative lookahead (?!).
> 
> I saw on this forum a post  "gnu.emacs.help ? regex nirvana - near miss"
> Where Drew Adams said: "Typically, what you want to do for this in Emacs Lisp is to combine 
> the use of a regexp for positive matching with other code that takes 
> care of the non-matching (negation) need. "
> 
> However, I'm not sure how to go about doing this.


If I'm not miss-understanding you completely, you want to match a non-
whitespace, followed either a whitespace or an end-of-line. That would be:

	[^[:space:]]\(?:[[:space:]]\|$\)

Am I missing something?


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

* Re: Negative Lookahead Equivalent in emacs
  2017-05-09  5:15 Negative Lookahead Equivalent in emacs luishenriquezperez
                   ` (2 preceding siblings ...)
  2017-05-09  9:54 ` Wasell
@ 2017-05-09 10:03 ` Felix Dietrich
  2017-05-09 13:50 ` luishenriquezperez
  2017-05-10 15:51 ` luishenriquezperez
  5 siblings, 0 replies; 9+ messages in thread
From: Felix Dietrich @ 2017-05-09 10:03 UTC (permalink / raw)
  To: help-gnu-emacs

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


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

* Re: Negative Lookahead Equivalent in emacs
  2017-05-09  5:15 Negative Lookahead Equivalent in emacs luishenriquezperez
                   ` (3 preceding siblings ...)
  2017-05-09 10:03 ` Felix Dietrich
@ 2017-05-09 13:50 ` luishenriquezperez
  2017-05-10  1:41   ` Emanuel Berg
  2017-05-10 15:51 ` luishenriquezperez
  5 siblings, 1 reply; 9+ messages in thread
From: luishenriquezperez @ 2017-05-09 13:50 UTC (permalink / raw)
  To: help-gnu-emacs

On Tuesday, May 9, 2017 at 1:15:46 AM UTC-4, luishenri...@gmail.com wrote:
> Hi,
> 
> 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]. 
> But I'm unsure of what the elisp equivalent (if any) of the negative lookahead (?!).
> 
> I saw on this forum a post  "gnu.emacs.help › regex nirvana - near miss"
> Where Drew Adams said: "Typically, what you want to do for this in Emacs Lisp is to combine 
> the use of a regexp for positive matching with other code that takes 
> care of the non-matching (negation) need. "
> 
> However, I'm not sure how to go about doing this.

Thank you this worked out.


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

* Re: Negative Lookahead Equivalent in emacs
  2017-05-09 13:50 ` luishenriquezperez
@ 2017-05-10  1:41   ` Emanuel Berg
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2017-05-10  1:41 UTC (permalink / raw)
  To: help-gnu-emacs

> Thank you this worked out.

How?

-- 
underground experts united
http://user.it.uu.se/~embe8573




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

* Re: Negative Lookahead Equivalent in emacs
  2017-05-09  9:54 ` Wasell
@ 2017-05-10 14:23   ` luishenriquezperez
  0 siblings, 0 replies; 9+ messages in thread
From: luishenriquezperez @ 2017-05-10 14:23 UTC (permalink / raw)
  To: help-gnu-emacs

On Tuesday, May 9, 2017 at 5:54:29 AM UTC-4, Wasell wrote:
> On Mon, 8 May 2017 22:15:42 -0700 (PDT), in article <49e1dd7d-4be5-4b03-b9e2-
> e26b15b0a6cb@googlegroups.com>, luishenriquezperez@gmail.com wrote:
> > 
> > Hi,
> > 
> > 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]. 
> > But I'm unsure of what the elisp equivalent (if any) of the negative lookahead (?!).
> > 
> > I saw on this forum a post  "gnu.emacs.help ? regex nirvana - near miss"
> > Where Drew Adams said: "Typically, what you want to do for this in Emacs Lisp is to combine 
> > the use of a regexp for positive matching with other code that takes 
> > care of the non-matching (negation) need. "
> > 
> > However, I'm not sure how to go about doing this.
> 
> 
> If I'm not miss-understanding you completely, you want to match a non-
> whitespace, followed either a whitespace or an end-of-line. That would be:
> 
> 	[^[:space:]]\(?:[[:space:]]\|$\)
> 
> Am I missing something?

No you're not missing anything. This one is a lot simpler :). 
Thank you.


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

* Re: Negative Lookahead Equivalent in emacs
  2017-05-09  5:15 Negative Lookahead Equivalent in emacs luishenriquezperez
                   ` (4 preceding siblings ...)
  2017-05-09 13:50 ` luishenriquezperez
@ 2017-05-10 15:51 ` luishenriquezperez
  5 siblings, 0 replies; 9+ messages in thread
From: luishenriquezperez @ 2017-05-10 15:51 UTC (permalink / raw)
  To: help-gnu-emacs

On Tuesday, May 9, 2017 at 1:15:46 AM UTC-4, luishenri...@gmail.com wrote:
> Hi,
> 
> 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]. 
> But I'm unsure of what the elisp equivalent (if any) of the negative lookahead (?!).
> 
> I saw on this forum a post  "gnu.emacs.help › regex nirvana - near miss"
> Where Drew Adams said: "Typically, what you want to do for this in Emacs Lisp is to combine 
> the use of a regexp for positive matching with other code that takes 
> care of the non-matching (negation) need. "
> 
> However, I'm not sure how to go about doing this.

I edited the solution slightly so no new lines are included.

\[^[:space:]\n\]\\(?:\[[:space:]\]\\|$\\)

There are a lot of backslashes because I used regex-builder to test it.


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

end of thread, other threads:[~2017-05-10 15:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-09  5:15 Negative Lookahead Equivalent in emacs luishenriquezperez
2017-05-09  7:16 ` Emanuel Berg
2017-05-09  8:44 ` hector
2017-05-09  9:54 ` Wasell
2017-05-10 14:23   ` luishenriquezperez
2017-05-09 10:03 ` Felix Dietrich
2017-05-09 13:50 ` luishenriquezperez
2017-05-10  1:41   ` Emanuel Berg
2017-05-10 15:51 ` luishenriquezperez

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.