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

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

Re: regexp question: match anything but not a group?


From: Thorsten Jolitz
Subject: Re: regexp question: match anything but not a group?
Date: Wed, 02 Apr 2014 09:21:23 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Thorsten Jolitz <tjolitz@gmail.com> writes:

> Hi List, 
>
> how can I write a regexp that acts like e.g.
>
> ,------
> | ".*?"
> `------
>
> but does not match a group like e.g. 
>
> ,---------------------
> | (regexp-quote "\\)")
> `---------------------
>
> ?
>
> This works more or less but does not seem to be very robust
>
> ,---------
> | "[^)]*?"
> `---------
>
> since ')' could appear in other contexts than the group. How can I
> negate a specific group of characters and not only any occurence of
> single characters?

I figured that I actually need something even smarter, because what I
really want is a regexp A that matches a given other regexp B if it is a
regexp-group, or not otherwise.

The best version of that regexp A I can come up with right now is
something like this:

#+begin_src emacs-lisp
  (concat "^"                         ; BOL
          (regexp-quote "\\(")        ; group begins
          "\\(\\?[[:digit:]]*:\\)?"   ; shy or explicitly numbered group?
          "[^\\000]+?"                ; any char, idea copied from org-mode
          (regexp-quote "\\)")        ; group ends
          "[*+]?[?]?"                 ; quantifier
          "$")                        ; EOL
#+end_src

The problem is that in the content part

,------------------------------------------------------------------
| "[^\\000]+?"                ; any char, idea copied from org-mode
`------------------------------------------------------------------

anything can happen, and any number of opening and/or closing parents
and sub-groups can appear, so I really need to determine if

,-----------------------------------------
| (regexp-quote "\\)")        ; group ends
`-----------------------------------------

closes 

,-------------------------------------------
| (regexp-quote "\\(")        ; group begins
`-------------------------------------------

and thats kind of hard to do with regexp syntax. 

I know now that I could simulate *look-ahead-assertions* for my original
problem, which aren't implemented in Emacs AFAIK, something on the line
of:

#+begin_src emacs-lisp
(progn
  (and (looking-at ".*")
       (not (eq (char-after) ?\))
       (not (eq (char-after (+ 1 (point)) MY-CHAR)))
       (not (eq (char-after (+ 2 (point)) MY-CHAR)))
       [...]
       ))
#+end_src

but counting and bookkeeping of opening and closing parens in regexp B
looks too difficult to me.

I can only imagine to check parens with lisp first (e.g. by using
`forward-sexp' or so) and then use a regexp like above that does not
care what is inside the group enclosing parens.

Then I could drop this part from the regexp too

,----------------------------------------------------------------
| "\\(\\?[[:digit:]]*:\\)?"   ; shy or explicitly numbered group?
`----------------------------------------------------------------

because all that counts are the matching parens.

Any ideas how to best check if a given regexp is a regexp group or not?

-- 
cheers,
Thorsten




reply via email to

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