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

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

Re: cleaning up a big regexp


From: Thorsten Jolitz
Subject: Re: cleaning up a big regexp
Date: Mon, 15 Sep 2014 16:35:15 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

address@hidden (Tory S. Anderson) writes:

> Using gnus I have a growing regexp that represents the criteria for bulk 
> email and splits accordingly:
>
> (setq my-gnus-bulk-regexp )
>
> ;; TODO make bulk-list
> (setq nnmail-split-methods
>        '(("mail.bulk" my-gnus-bulk-regexp)
>         ...
>  
>
> Is there a way to clean this up to make it both more readable and more
> easily editable? It seems like keeping some kind of list would be the
> way to do it, instead of an ever-lengthening string. 

I have a little library called drx.el on github
(https://github.com/tj64/drx ) which isn't as extreme as rx.el in
avoiding regexp strings, but is pretty flexible when it comes to avoid
all the plumbing/glue-code between the actual regexp strings.

Furthermore it abstracts from 3 common regexp elements: "^", "$" and
"\\*", so it lets you e.g. write a regexp for Org-mode:


#+BEGIN_SRC emacs-lisp :results raw
  (require 'drx)
  (format "%S"
   (drx "foo" t '(t nil (2)) t '(nil t (2))
         "bar" "loo"))
#+END_SRC

#+results:
"^\\(\\*\\(\\*\\)\\{2\\}\\)foo\\(bar\\)\\(loo\\)\\{2\\}$"

and reuse it for css mode with different outline stars:

#+BEGIN_SRC emacs-lisp :results raw
  (require 'drx)
  (let ((drx-BOL "^/\\*  ")
        (drx-EOL " \\*/$")
        (drx-STAR "@"))
  (format "%S"
   (drx "foo" t '(t nil (2)) t '(nil t (2))
         "bar" "loo")))
#+END_SRC

#+results:
"^/\\*  \\(@\\(@\\)\\{2\\}\\)foo\\(bar\\)\\(loo\\)\\{2\\} \\*/$"

Here is something similar to your example regexp:

#+BEGIN_SRC emacs-lisp :results raw
  (require 'drx)
  (format "%S"
   (drx 
    (drx "From:.*" t nil nil 'group
        (drx "@maillist.codeproject.com" nil nil nil 'alt    
             "@papajohns-specials.com"
             "@qomail.quikorder.com"
             "@linkedin.com"
             "@facebookmail.com"
             "@plus.google.com"
             "@twitter.com"
             "@youtube.com"
             "@linguistlist.org"
             "sportsauthority.com"))
    nil nil nil 'alt
    (drx "To:address@hidden" nil nil nil t)))
#+END_SRC

#+results:
"\\(^\\(From:.*\\(@maillist.codeproject.com\\|@papajohns-specials.com\\|@qomail.quikorder.com\\|@linkedin.com\\|@facebookmail.com\\|@plus.google.com\\|@twitter.com\\|@youtube.com\\|@linguistlist.org\\|sportsauthority.com\\)\\)\\|\\(To:address@hidden)\\)"

-- 
cheers,
Thorsten




reply via email to

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