unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to combine these two regular expressions
@ 2012-08-12 14:57 Cecil Westerhof
  2012-08-12 18:44 ` Jambunathan K
  2012-08-13 14:08 ` Doug Lewan
  0 siblings, 2 replies; 3+ messages in thread
From: Cecil Westerhof @ 2012-08-12 14:57 UTC (permalink / raw)
  To: help-gnu-emacs

I was asked by someone for a regular expression that could combine
several search patterns, so that a file only needs to be scanned ones
and not several times. I was partly successful, but I still need two
passes.

I now have the following code:
    (defun dcbl-search-heading ()
      (interactive)
      (re-search-forward "\\(.
    \\)\\(

    \\)\\(.+
    \\)\\(
    ?.+
    \\)" nil t)
      (replace-match "\\1
    #heading#
    \\4"))

    (defun dcbl-search-heading2 ()
      (interactive)
      (re-search-forward "\\(.
    \\)\\(
    \\)\\(.+
    \\)\\(\\\(.+
    \\\)+\\)" nil t)
      (replace-match "\\1
    #heading#
    \\4"))

Is there a way to combine those regular expressions to one?

By the way, if the used expressions could be optimised, let me know. I
do not make them often, thus maybe I do things sub-optimal here.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


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

* Re: How to combine these two regular expressions
  2012-08-12 14:57 How to combine these two regular expressions Cecil Westerhof
@ 2012-08-12 18:44 ` Jambunathan K
  2012-08-13 14:08 ` Doug Lewan
  1 sibling, 0 replies; 3+ messages in thread
From: Jambunathan K @ 2012-08-12 18:44 UTC (permalink / raw)
  To: Cecil Westerhof; +Cc: help-gnu-emacs


You should provide a human-readable description of what the regexes are
trying to do.  Have you tried:

C-h f regexp-opt RET

Cecil Westerhof <Cecil@decebal.nl> writes:

> I was asked by someone for a regular expression that could combine
> several search patterns, so that a file only needs to be scanned ones
> and not several times. I was partly successful, but I still need two
> passes.
>
> I now have the following code:
>     (defun dcbl-search-heading ()
>       (interactive)
>       (re-search-forward "\\(.
>     \\)\\(
>
>     \\)\\(.+
>     \\)\\(
>     ?.+
>     \\)" nil t)
>       (replace-match "\\1
>     #heading#
>     \\4"))
>
>     (defun dcbl-search-heading2 ()
>       (interactive)
>       (re-search-forward "\\(.
>     \\)\\(
>     \\)\\(.+
>     \\)\\(\\\(.+
>     \\\)+\\)" nil t)
>       (replace-match "\\1
>     #heading#
>     \\4"))
>
> Is there a way to combine those regular expressions to one?
>
> By the way, if the used expressions could be optimised, let me know. I
> do not make them often, thus maybe I do things sub-optimal here.

-- 



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

* RE: How to combine these two regular expressions
  2012-08-12 14:57 How to combine these two regular expressions Cecil Westerhof
  2012-08-12 18:44 ` Jambunathan K
@ 2012-08-13 14:08 ` Doug Lewan
  1 sibling, 0 replies; 3+ messages in thread
From: Doug Lewan @ 2012-08-13 14:08 UTC (permalink / raw)
  To: Cecil Westerhof, help-gnu-emacs@gnu.org

The most common way to "combine" two regular expressions is the form "\\(RE1\\|RE2\\)".

I usually do this with variables (concat "\\(" re-1 "\\|" re-2 "\\)").

There's also the form "\\(?:...\\)" if you don't care about match data or "\\(?N:...\\)" if you care very much about it. (N is an integer and forces this match to become match number N.)

I hope this helps.

,Doug

> -----Original Message-----
> From: help-gnu-emacs-bounces+dougl=shubertticketing.com@gnu.org
> [mailto:help-gnu-emacs-bounces+dougl=shubertticketing.com@gnu.org] On
> Behalf Of Cecil Westerhof
> Sent: Sunday, 2012 August 12 10:57
> To: help-gnu-emacs@gnu.org
> Subject: How to combine these two regular expressions
> 
> I was asked by someone for a regular expression that could combine
> several search patterns, so that a file only needs to be scanned ones
> and not several times. I was partly successful, but I still need two
> passes.
> 
> I now have the following code:
>     (defun dcbl-search-heading ()
>       (interactive)
>       (re-search-forward "\\(.
>     \\)\\(
> 
>     \\)\\(.+
>     \\)\\(
>     ?.+
>     \\)" nil t)
>       (replace-match "\\1
>     #heading#
>     \\4"))
> 
>     (defun dcbl-search-heading2 ()
>       (interactive)
>       (re-search-forward "\\(.
>     \\)\\(
>     \\)\\(.+
>     \\)\\(\\\(.+
>     \\\)+\\)" nil t)
>       (replace-match "\\1
>     #heading#
>     \\4"))
> 
> Is there a way to combine those regular expressions to one?
> 
> By the way, if the used expressions could be optimised, let me know. I
> do not make them often, thus maybe I do things sub-optimal here.
> 
> --
> Cecil Westerhof
> Senior Software Engineer
> LinkedIn: http://www.linkedin.com/in/cecilwesterhof



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

end of thread, other threads:[~2012-08-13 14:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-12 14:57 How to combine these two regular expressions Cecil Westerhof
2012-08-12 18:44 ` Jambunathan K
2012-08-13 14:08 ` Doug Lewan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).