all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* On refining regexp by adding exceptions systematically
@ 2002-10-03 12:27 gnuist
  2002-10-03 18:38 ` Kaz Kylheku
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: gnuist @ 2002-10-03 12:27 UTC (permalink / raw)


Here is regular expression in emacs lisp that initially seems to work
for the job:

[A-Z][A-Z][A-Z][0-9]+


After running it on a number of uses, I find that 
there is an exception to it, namely PJP89898. 
Rather than rehashing the code after having forgotten it
and reworking my regexp expression
(every time I find an exception) in some convoluted way, is
there a systematic way to add an exception or a series of
exceptions to the regexp? I am sure that there are a number
of ways to do this and each has its merits.

I am using this regexp in two ways in a different program.
In the first one (looking-at regexp) so that it assumes that
cursor is on it. In the second one (search-forward-regexp regexp)
in a narrowed region so that one is trying to find if there is one.
It seems to me that it is a little tricky to do this. Perhaps an
example code would help with exception implemented for searching
on a line.

Thanks a lot!
gnuist007

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

* Re: On refining regexp by adding exceptions systematically
  2002-10-03 12:27 On refining regexp by adding exceptions systematically gnuist
@ 2002-10-03 18:38 ` Kaz Kylheku
  2002-10-03 23:04   ` Kaz Kylheku
       [not found]   ` <tvikna.obd.ln@lart.ca>
  2002-10-05 13:31 ` Alan Mackenzie
  2002-10-05 19:46 ` Christopher Browne
  2 siblings, 2 replies; 10+ messages in thread
From: Kaz Kylheku @ 2002-10-03 18:38 UTC (permalink / raw)


gnuist007@hotmail.com (gnuist) wrote in message news:<9e8ebeb2.0210030427.4544cb00@posting.google.com>...
> Here is regular expression in emacs lisp that initially seems to work
> for the job:

Goddamned idiot, you have already been advised not to crosspost Lisp
questions to comp.lang.lisp. If you can't understand a simple thing
like that, you aren't fit to participate in any activity that requires
thought.

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

* Re: On refining regexp by adding exceptions systematically
  2002-10-03 18:38 ` Kaz Kylheku
@ 2002-10-03 23:04   ` Kaz Kylheku
       [not found]   ` <tvikna.obd.ln@lart.ca>
  1 sibling, 0 replies; 10+ messages in thread
From: Kaz Kylheku @ 2002-10-03 23:04 UTC (permalink / raw)


kaz@ashi.footprints.net (Kaz Kylheku) wrote in message news:<cf333042.0210031038.56c8ee4e@posting.google.com>...
> gnuist007@hotmail.com (gnuist) wrote in message news:<9e8ebeb2.0210030427.4544cb00@posting.google.com>...
> > Here is regular expression in emacs lisp that initially seems to work
> > for the job:
> 
> Goddamned idiot, you have already been advised not to crosspost Lisp
                                                                  ^Emacs

> questions to comp.lang.lisp. 

:)

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

* Re: On refining regexp by adding exceptions systematically
  2002-10-03 12:27 On refining regexp by adding exceptions systematically gnuist
  2002-10-03 18:38 ` Kaz Kylheku
@ 2002-10-05 13:31 ` Alan Mackenzie
  2002-10-05 16:11   ` Stefan Monnier <foo@acm.com>
  2002-10-06  3:02   ` Vassil Nikolov
  2002-10-05 19:46 ` Christopher Browne
  2 siblings, 2 replies; 10+ messages in thread
From: Alan Mackenzie @ 2002-10-05 13:31 UTC (permalink / raw)


gnuist <gnuist007@hotmail.com> wrote on 3 Oct 2002 05:27:55 -0700:
> Here is regular expression in emacs lisp that initially seems to work
> for the job:

> [A-Z][A-Z][A-Z][0-9]+

> After running it on a number of uses, I find that there is an exception
> to it, namely PJP89898.   Rather than rehashing the code after having
> forgotten it and reworking my regexp expression (every time I find an
> exception) in some convoluted way, is there a systematic way to add an
> exception or a series of exceptions to the regexp? I am sure that there
> are a number of ways to do this and each has its merits.

Regular expressions are designed to find string expressions which are,
well, regular.  If you really want to add in an exception like you've
got, you're going to end up with something horrible.  It can be done, but
like rowing the Atlantic, why bother?

> I am using this regexp in two ways in a different program.  In the
> first one (looking-at regexp) so that it assumes that cursor is on it.

(and (looking-at regexp) (not (looking-at "PJP89898")))

> In the second one (search-forward-regexp regexp) in a narrowed region
> so that one is trying to find if there is one.  It seems to me that it
> is a little tricky to do this. Perhaps an example code would help with
> exception implemented for searching on a line.

(let (found (startpos (point)))
  (while (and (setq found (search-forward-regexp regexp nil t))
              (save-excursion
                (goto-char (match-data 0))
                (looking-at "PJP89898"))))
  (if found (point)
   (goto-char startpos) nil))

I haven't tested either snippet.

Hope this helps.

By the way, it would be helpful if you could set a Followup-To: header,
so that people know what your "home" group is.  It also makes you look
less like a troll.

> Thanks a lot!
> gnuist007

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

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

* Re: On refining regexp by adding exceptions systematically
  2002-10-05 13:31 ` Alan Mackenzie
@ 2002-10-05 16:11   ` Stefan Monnier <foo@acm.com>
  2002-10-05 17:45     ` Alan Mackenzie
  2002-10-06  3:02   ` Vassil Nikolov
  1 sibling, 1 reply; 10+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2002-10-05 16:11 UTC (permalink / raw)


>> Here is regular expression in emacs lisp that initially seems to work
>> for the job:
>> [A-Z][A-Z][A-Z][0-9]+
>> After running it on a number of uses, I find that there is an exception
>> to it, namely PJP89898.   Rather than rehashing the code after having
>> forgotten it and reworking my regexp expression (every time I find an
>> exception) in some convoluted way, is there a systematic way to add an
>> exception or a series of exceptions to the regexp? I am sure that there
>> are a number of ways to do this and each has its merits.
> Regular expressions are designed to find string expressions which are,
> well, regular.  If you really want to add in an exception like you've
> got, you're going to end up with something horrible.  It can be done, but
> like rowing the Atlantic, why bother?

Actually, the end would still be regular.  But it's not currently supported
by Emacs' regexp engine.
Oh and BTW, extended regular expressions (as seen in Emacs and egrep)
are actually not regular because of back-references.
Perl regexps have support for things like that.


        Stefan

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

* Re: On refining regexp by adding exceptions systematically
  2002-10-05 16:11   ` Stefan Monnier <foo@acm.com>
@ 2002-10-05 17:45     ` Alan Mackenzie
  0 siblings, 0 replies; 10+ messages in thread
From: Alan Mackenzie @ 2002-10-05 17:45 UTC (permalink / raw)


"Stefan Monnier <foo@acm.com>"
<monnier+gnu.emacs.help/news/@flint.cs.yale.edu> wrote on 05 Oct 2002
12:11:30 -0400:
>>> Here is regular expression in emacs lisp that initially seems to work
>>> for the job: [A-Z][A-Z][A-Z][0-9]+

>>> After running it on a number of uses, I find that there is an exception
>>> to it, namely PJP89898.   Rather than rehashing the code after having
>>> forgotten it and reworking my regexp expression (every time I find an
>>> exception) in some convoluted way, is there a systematic way to add an
>>> exception or a series of exceptions to the regexp? I am sure that there
>>> are a number of ways to do this and each has its merits.

>> Regular expressions are designed to find string expressions which are,
>> well, regular.  If you really want to add in an exception like you've
>> got, you're going to end up with something horrible.  It can be done,
>> but like rowing the Atlantic, why bother?

> Actually, the end would still be regular.  But it's not currently
> supported by Emacs' regexp engine.

Sorry, what's not supported?

> Oh and BTW, extended regular expressions (as seen in Emacs and egrep)
> are actually not regular because of back-references.  Perl regexps have
> support for things like that.

Hmm.  Thanks, Stefan!  ;-)  I was just trying to help the guy.

I'm not actually that well versed with the theory behind regexps.  I read
somewhere (that book with the young woman operating a Heath-Robinson
contraption on the front cover, and the same thing broken on the back
cover) that a regexp is equivalent to a finite-state-machine, in that if
one of either of them can recognise a string, so can one of the other.

Nevertheless, I think I know how to use them, more or less, and what
they're good for, and what they're not good for.  On their own, they're
not good for the original poster's problem.

>         Stefan

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

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

* Re: On refining regexp by adding exceptions systematically
  2002-10-03 12:27 On refining regexp by adding exceptions systematically gnuist
  2002-10-03 18:38 ` Kaz Kylheku
  2002-10-05 13:31 ` Alan Mackenzie
@ 2002-10-05 19:46 ` Christopher Browne
  2 siblings, 0 replies; 10+ messages in thread
From: Christopher Browne @ 2002-10-05 19:46 UTC (permalink / raw)


Oops! gnuist007@hotmail.com (gnuist) was seen spray-painting on a wall:
> Here is regular expression in emacs lisp that initially seems to work
> for the job:
> [A-Z][A-Z][A-Z][0-9]+

> After running it on a number of uses, I find that there is an
> exception to it, namely PJP89898.  Rather than rehashing the code
> after having forgotten it and reworking my regexp expression (every
> time I find an exception) in some convoluted way, is there a
> systematic way to add an exception or a series of exceptions to the
> regexp? I am sure that there are a number of ways to do this and
> each has its merits.

> I am using this regexp in two ways in a different program.  In the
> first one (looking-at regexp) so that it assumes that cursor is on
> it. In the second one (search-forward-regexp regexp) in a narrowed
> region so that one is trying to find if there is one.  It seems to
> me that it is a little tricky to do this. Perhaps an example code
> would help with exception implemented for searching on a line.

If your specification sucks, "adding exceptions" is just going to make
the code "suck worse" as you find more of them.

Maybe you need to do the design work up front to determine the /real/
specification for whatever it was that you were searching for.

And perhaps you should set up followup to your /favorite/ newsgroup?
This isn't really a Unix question, nor is it a Lisp question, so
discussion probably shouldn't continue in these newsgroups.
-- 
(reverse (concatenate 'string "moc.enworbbc@" "sirhc"))
http://cbbrowne.com/info/emacs.html
"Necessity is the mother of invention" is a silly proverb.  "Necessity
is the  mother of  futile dodges"  is much closer  to the  truth.  The
basis of growth of modern  invention is science, and science is almost
wholly the outgrowth of pleasurable intellectual curiosity.
-- Alfred N. Whitehead

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

* Re: On refining regexp by adding exceptions systematically
  2002-10-05 13:31 ` Alan Mackenzie
  2002-10-05 16:11   ` Stefan Monnier <foo@acm.com>
@ 2002-10-06  3:02   ` Vassil Nikolov
  2002-10-06  7:48     ` Alan Mackenzie
  1 sibling, 1 reply; 10+ messages in thread
From: Vassil Nikolov @ 2002-10-06  3:02 UTC (permalink / raw)


    On Sat, 5 Oct 2002 13:31:24 +0000, Alan Mackenzie<none@example.invalid> said:

    AM> Regular expressions are designed to find string expressions which are,
    AM> well, regular.

A minor note: IIRC, `regular' means that the language defined by
the expression can be handled by a finite automaton (a stack is not
needed).

---Vassil.

-- 
Garbage collection is charged at 0.19e-9 cents a cons.  Bulk rates
are also available: please contact memory management for details.

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

* Re: On refining regexp by adding exceptions systematically
  2002-10-06  3:02   ` Vassil Nikolov
@ 2002-10-06  7:48     ` Alan Mackenzie
  0 siblings, 0 replies; 10+ messages in thread
From: Alan Mackenzie @ 2002-10-06  7:48 UTC (permalink / raw)


Vassil Nikolov <vnikolov@poboxes.com> wrote on 05 Oct 2002 23:02:02
-0400:
>     On Sat, 5 Oct 2002 13:31:24 +0000, Alan
>     Mackenzie<none@example.invalid> said:

>   AM> Regular expressions are designed to find string expressions which are,
>   AM> well, regular.

> A minor note: IIRC, `regular' means that the language defined by the
> expression can be handled by a finite automaton (a stack is not
> needed).

Hmmm.  I was sort of dimly aware of that when I wrote the above.  What's
really needed is word meaning "devoid of grotesquely shaped sticky-out
bits" but which isn't "regular".

> ---Vassil.

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

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

* Re: On refining regexp by adding exceptions systematically
       [not found]   ` <tvikna.obd.ln@lart.ca>
@ 2002-10-07 14:13     ` Rodney Sparapani
  0 siblings, 0 replies; 10+ messages in thread
From: Rodney Sparapani @ 2002-10-07 14:13 UTC (permalink / raw)


Sorry, I missed the original post.  I wanted the same functionality from 
regexps myself. However, after I realized that regexps alone would not 
do it, I wrote the following routine in elisp for ESS.  Enjoy.

(defun ess-search-except (regexp &optional except backward)
"Search for a regexp, store as match 1, optionally ignore strings that 
match exceptions."
    (interactive)
   
    (let ((continue t) (exit nil))

    (while continue
    (if (or (and backward (search-backward-regexp regexp nil t))
                (and (not backward) (search-forward-regexp regexp nil 
t))) (progn
        (setq exit (match-string 1))
            (setq continue (and except (string-match except exit)))
        (if continue (setq exit nil)))
        ;else
        (setq continue nil)))

    exit))

-- 
Rodney Sparapani              Medical College of Wisconsin
Sr. Biostatistician           Patient Care & Outcomes Research
rsparapa@mcw.edu              http://www.mcw.edu/pcor
Was 'Name That Tune' rigged?  WWLD -- What Would Lombardi Do

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

end of thread, other threads:[~2002-10-07 14:13 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-10-03 12:27 On refining regexp by adding exceptions systematically gnuist
2002-10-03 18:38 ` Kaz Kylheku
2002-10-03 23:04   ` Kaz Kylheku
     [not found]   ` <tvikna.obd.ln@lart.ca>
2002-10-07 14:13     ` Rodney Sparapani
2002-10-05 13:31 ` Alan Mackenzie
2002-10-05 16:11   ` Stefan Monnier <foo@acm.com>
2002-10-05 17:45     ` Alan Mackenzie
2002-10-06  3:02   ` Vassil Nikolov
2002-10-06  7:48     ` Alan Mackenzie
2002-10-05 19:46 ` Christopher Browne

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.