unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Doug Lewan <dougl@shubertticketing.com>
To: Cecil Westerhof <Cecil@decebal.nl>,
	"help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: RE: How to combine these two regular expressions
Date: Mon, 13 Aug 2012 14:08:26 +0000	[thread overview]
Message-ID: <155DEC68569B714B86C2C7075F5EDA98268CCDB0@DAKIYA1.pegasus.local> (raw)
In-Reply-To: <87d32w88fl.fsf@Compaq.site>

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



      parent reply	other threads:[~2012-08-13 14:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=155DEC68569B714B86C2C7075F5EDA98268CCDB0@DAKIYA1.pegasus.local \
    --to=dougl@shubertticketing.com \
    --cc=Cecil@decebal.nl \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).