[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: How to combine these two regular expressions
From: |
Doug Lewan |
Subject: |
RE: How to combine these two regular expressions |
Date: |
Mon, 13 Aug 2012 14:08:26 +0000 |
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