Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

does emacs regular expression support (?!expression)

198 views
Skip to first unread message

土星五号

unread,
Feb 2, 2012, 9:02:08 AM2/2/12
to help-gn...@gnu.org
does emacs regular expression support (?!expression)  ?

Tassilo Horn

unread,
Feb 2, 2012, 1:56:51 PM2/2/12
to help-gn...@gnu.org
土星五号 <laf...@gmail.com> writes:

> does emacs regular expression support (?!expression) ?

It would be more helpful if you'd tell us what (?!expression) would
match, preferably with some examples. Syntactically, emacs regexp's shy
groups look similar. The regular expression

"\\(:?foo\\|bar\\)\\([0-9]+\\)"

matches "foo19" or "bar23", but doesn't capture foo or bar, so that
(match-string 1) is the number.

Bye,
Tassilo


Tim Landscheidt

unread,
Feb 2, 2012, 4:14:40 PM2/2/12
to help-gn...@gnu.org
Tassilo Horn <tas...@member.fsf.org> wrote:

>> does emacs regular expression support (?!expression) ?

> It would be more helpful if you'd tell us what (?!expression) would
> match, preferably with some examples. Syntactically, emacs regexp's shy
> groups look similar. The regular expression

> "\\(:?foo\\|bar\\)\\([0-9]+\\)"

> matches "foo19" or "bar23", but doesn't capture foo or bar, so that
> (match-string 1) is the number.

In Perl, "(?!pattern)" is a zero-width negative look-ahead
assertion. Emacs does not support these AFAIK.

Tim


Tassilo Horn

unread,
Feb 2, 2012, 4:45:04 PM2/2/12
to help-gn...@gnu.org
Tim Landscheidt <t...@tim-landscheidt.de> writes:

Hi Tim,

>> It would be more helpful if you'd tell us what (?!expression) would
>> match, preferably with some examples. Syntactically, emacs regexp's shy
>> groups look similar. The regular expression
>
>> "\\(:?foo\\|bar\\)\\([0-9]+\\)"

BTW, that should have been (?:...).

>> matches "foo19" or "bar23", but doesn't capture foo or bar, so that
>> (match-string 1) is the number.
>
> In Perl, "(?!pattern)" is a zero-width negative look-ahead
> assertion. Emacs does not support these AFAIK.

I see. So when you do /foo(?!bar)/ in Perl, you'd need to do
"foo\\(?:[^b][^a][^r]\\)" in elisp.

Bye,
Tassilo


Glenn Morris

unread,
Feb 2, 2012, 5:09:43 PM2/2/12
to help-gn...@gnu.org
Tim Landscheidt wrote:

> In Perl, "(?!pattern)" is a zero-width negative look-ahead
> assertion. Emacs does not support these AFAIK.

http://debbugs.gnu.org/cgi/bugreport.cgi?bug=5393
(to date, not applied)

See also the linked
http://lists.gnu.org/archive/html/emacs-devel/2012-01/msg00732.html


Tim Landscheidt

unread,
Feb 2, 2012, 5:11:27 PM2/2/12
to help-gn...@gnu.org
Tassilo Horn <tas...@member.fsf.org> wrote:

> [...]

>>> matches "foo19" or "bar23", but doesn't capture foo or bar, so that
>>> (match-string 1) is the number.

>> In Perl, "(?!pattern)" is a zero-width negative look-ahead
>> assertion. Emacs does not support these AFAIK.

> I see. So when you do /foo(?!bar)/ in Perl, you'd need to do
> "foo\\(?:[^b][^a][^r]\\)" in elisp.

You have to account for possible end-of-buffer as well so
usually it's "easier" to use two matches ('(while (and
(search-forward-regexp "foo") (not (looking-at "bar"))))'
(untested)) - that's why I (still :-)) think these features
should be implemented in Emacs as well.

Tim


Tim Landscheidt

unread,
Feb 2, 2012, 5:17:04 PM2/2/12
to help-gn...@gnu.org
I wrote:

>> [...]

>>>> matches "foo19" or "bar23", but doesn't capture foo or bar, so that
>>>> (match-string 1) is the number.

>>> In Perl, "(?!pattern)" is a zero-width negative look-ahead
>>> assertion. Emacs does not support these AFAIK.

>> I see. So when you do /foo(?!bar)/ in Perl, you'd need to do
>> "foo\\(?:[^b][^a][^r]\\)" in elisp.

> You have to account for possible end-of-buffer as well so
> usually it's "easier" to use two matches ('(while (and
> (search-forward-regexp "foo") (not (looking-at "bar"))))'
> (untested)) - [...]

Eh, yes, there is an "(untested)", but still the logic is
obviously plain wrong. But you get the idea.

Tim


Kevin Rodgers

unread,
Feb 2, 2012, 7:37:57 PM2/2/12
to help-gn...@gnu.org
On 2/2/12 2:45 PM, Tassilo Horn wrote:
> Tim Landscheidt<t...@tim-landscheidt.de> writes:

>> In Perl, "(?!pattern)" is a zero-width negative look-ahead
>> assertion. Emacs does not support these AFAIK.
>
> I see. So when you do /foo(?!bar)/ in Perl, you'd need to do
> "foo\\(?:[^b][^a][^r]\\)" in elisp.

IIUC, the Perl regex would successfully match "foo" if it were followed by
"far", but the Emacs regexp would not.

Maybe \(?:[^b][^a][^r]\) should be \(?:[^b]\|b[^a]\|ba[^r]\)

--
Kevin Rodgers
Denver, Colorado, USA


土星五号

unread,
Feb 2, 2012, 9:12:27 PM2/2/12
to Kevin Rodgers, help-gn...@gnu.org
I would like match Err in any words(e.g. LastError), but not ErrorMode.
(defvar txt-mode-font-lock-keywords
  `(
    ;; 文件名
    ("\\\\\\(\\w+\\.exe\\)" 1 font-lock-keyword-face)
    ;; IP和版本
    ("[^0-9]\\([0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\)" 1 font-lock-keyword-face)
    ;; Error
    ("\\<\\([Dd][Bb][Ee][Rr][Rr][Oo][Rr]\\)\\>" 1 font-lock-warning-face)
    ("\\<\\([Dd][Bb][Ee][Rr][Rr]\\)\\>" 1 font-lock-warning-face)
    ("\\<\\([Ee][Rr][Rr][Oo][Rr]\\)\\>" 1 font-lock-warning-face)
    ("\\([Ee][Rr][Rr]\\)\\(?!orMode\\)" 1 font-lock-warning-face)
    ("\\(错误\\)" 1 font-lock-warning-face)
    ("\\(失败\\)" 1 font-lock-warning-face)
    ("\\(严重\\)" 1 font-lock-warning-face)
    ("\\<\\([Ff][Aa][Ii][Ll][Ee][Dd]\\)\\>" 1 font-lock-warning-face)
    ("\\<\\([Ff][Aa][Ii][Ll]\\)\\>" 1 font-lock-warning-face)
    ("\\<\\([Cc][Aa][Nn]\\s+[Nn][Oo][Tt]\\)\\>" 1 font-lock-warning-face)
    ("\\<\\([Cc][Aa][Nn]'[Tt]\\)\\>" 1 font-lock-warning-face)
    ("\\<\\([Ww][Rr][Oo][Nn][Gg]\\)\\>" 1 font-lock-warning-face)
    ;; Warning
    ("\\<\\([Ww][Aa][Rr][Nn][Ii][Nn][Gg]\\)\\>" 1 font-lock-warning-face)
    ("\\<\\([Ww][Aa][Rr][Nn]\\)\\>" 1 font-lock-warning-face)
    ("\\(警告\\)" 1 font-lock-warning-face)
    ;; Exception
    ("\\<\\([Ee][Xx][Cc][Ee][Pp][Tt][Ii][Oo][Nn]\\)\\>" 1 font-lock-warning-face)
    ("\\<\\([Ee][Xx][Cc][Ee][Pp][Tt]\\)\\>" 1 font-lock-warning-face)
    ("\\(异常\\)" 1 font-lock-warning-face)
    )
  "Basic font lock keywords for txt mode.  Highlights keywords.")

2012/2/3 Kevin Rodgers <kevin.d...@gmail.com>
On 2/2/12 2:45 PM, Tassilo Horn wrote:
Tim Landscheidt<tim@tim-landscheidt.de>  writes:

Thien-Thi Nguyen

unread,
Feb 3, 2012, 3:30:42 AM2/3/12
to 土星五号, help-gn...@gnu.org
() 土星五号 <laf...@gmail.com>
() Fri, 3 Feb 2012 10:12:27 +0800

I would like match Err in any words(e.g. LastError), but not ErrorMode.

("\\([Ee][Rr][Rr]\\)\\(?!orMode\\)" 1 font-lock-warning-face)

You can add a spec for ‘ErrorMode’, with default face, prior to this one.

Tim Landscheidt

unread,
Feb 3, 2012, 6:17:25 AM2/3/12
to help-gn...@gnu.org
Thien-Thi Nguyen <t...@gnuvola.org> wrote:

> I would like match Err in any words(e.g. LastError), but not ErrorMode.

> ("\\([Ee][Rr][Rr]\\)\\(?!orMode\\)" 1 font-lock-warning-face)

> You can add a spec for ‘ErrorMode’, with default face, prior to this one.

Which Emacs version does support that?

Tim


Thien-Thi Nguyen

unread,
Feb 3, 2012, 6:48:11 AM2/3/12
to help-gn...@gnu.org
() Tim Landscheidt <t...@tim-landscheidt.de>
() Fri, 03 Feb 2012 11:17:25 +0000

> You can add a spec for ‘ErrorMode’, with default face, prior to this one.

Which Emacs version does support that?

Any that processes these specs in the given order, with
"masking semantics", i suppose. (I haven't tried.)

Tim Landscheidt

unread,
Feb 4, 2012, 5:12:33 AM2/4/12
to help-gn...@gnu.org
Thien-Thi Nguyen <t...@gnuvola.org> wrote:

> > You can add a spec for ‘ErrorMode’, with default face, prior to this one.

> Which Emacs version does support that?

> Any that processes these specs in the given order, with
> "masking semantics", i suppose. (I haven't tried.)

What does:

| (search-forward-regexp "\\([Ee][Rr][Rr]\\)\\(?!orMode\\)")

give on your Emacs?

Tim


土星五号

unread,
Feb 4, 2012, 7:16:29 AM2/4/12
to help-gn...@gnu.org
No, i put it in txt-mode-load.el.

 .emacs:
(require 'txt-mode-load)



2012/2/4 Tim Landscheidt <t...@tim-landscheidt.de>

Tim Landscheidt

unread,
Feb 5, 2012, 6:57:41 AM2/5/12
to help-gn...@gnu.org
Thien-Thi Nguyen <t...@gnuvola.org> wrote:

> [...]
> It throws an error "Invalid regular expression".
> Sorry for being unclear. [...]

Thanks.

Tim


David Combs

unread,
Mar 10, 2012, 8:53:01 PM3/10/12
to
In article <mailman.3005.132822070...@gnu.org>,
And, by the above, that's not a "regexp" (emacs), but a bunch of
elisp code. Meaning that (as far as I know) you can't just
type it in, in response to a prompt-for-a-regexp, as you'd get
with a M-C s, query-replace-regexp, etc, but would have to --
well, what *would* you have to do for a M-C % -- you would have
to write a whole paragraph of elisp to duplicate what query-replace-regexp
does, somewhere inserting the above elisp that emulates the perl
regexp?

Easier to just get out of emacs for a bit, and do that perl-like
edit in perl itself?

David




0 new messages