From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: John Mastro Newsgroups: gmane.emacs.help Subject: Re: Tricky Regexp - How to insert a marker every 3rd number in a sequence that begins with a certain delimiter Date: Sat, 6 Jun 2015 12:08:55 -0700 Message-ID: References: <2df2916e-fcff-4e84-ba5c-bf6b492b6554@googlegroups.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1433617782 20792 80.91.229.3 (6 Jun 2015 19:09:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 6 Jun 2015 19:09:42 +0000 (UTC) To: gnuist006@gmail.com, "help-gnu-emacs@gnu.org" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jun 06 21:09:34 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Z1JTE-0000Ey-Rx for geh-help-gnu-emacs@m.gmane.org; Sat, 06 Jun 2015 21:09:29 +0200 Original-Received: from localhost ([::1]:52368 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z1JTD-00067t-FN for geh-help-gnu-emacs@m.gmane.org; Sat, 06 Jun 2015 15:09:27 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z1JT3-00067g-NP for help-gnu-emacs@gnu.org; Sat, 06 Jun 2015 15:09:18 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z1JT2-0000bH-Pf for help-gnu-emacs@gnu.org; Sat, 06 Jun 2015 15:09:17 -0400 Original-Received: from mail-ob0-x22e.google.com ([2607:f8b0:4003:c01::22e]:33543) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z1JT2-0000b7-HZ for help-gnu-emacs@gnu.org; Sat, 06 Jun 2015 15:09:16 -0400 Original-Received: by obcej4 with SMTP id ej4so23576840obc.0 for ; Sat, 06 Jun 2015 12:09:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=8Kc7WOyz5zxjDJQSlzSc1J8q+ndqY7nmOo9AqPqDx+M=; b=nVYVnAB364midO5DstSRZ2KXNWBWWA5c1uUJ7zvn06iTXsuJ6n23bfeobLuaQLD5nz RjBFG/ife69eeeeLBRGnLwdAWEObmpKcQJRR2rnj0tRJ0nfgz20jDGcUXJbkApqtnoIm xZdDl3F+I0ncVtrMSS87JcGm6F6KFEQQ7HWZmIH2TrTPicIcDJyUyei861Ovjftfv1nU L/GFafxBiCdA6b0xYyZIZS97ZNjID1dAJbJDOMwvzcPM/3DDqOJsGY7R9Y97XcOac7Af 6OD58KeTElT2G8OnQQ0xeGBrR5KWtdwiJVeNQ34Jlul+XZOelP7IO+RiRJKSCC82HZn2 4x/Q== X-Received: by 10.182.236.5 with SMTP id uq5mr8086582obc.13.1433617755806; Sat, 06 Jun 2015 12:09:15 -0700 (PDT) Original-Received: by 10.76.168.70 with HTTP; Sat, 6 Jun 2015 12:08:55 -0700 (PDT) In-Reply-To: <2df2916e-fcff-4e84-ba5c-bf6b492b6554@googlegroups.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c01::22e X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:104798 Archived-At: wrote: > tricky regexp > > How to insert a marker every 3rd number in a sequence that begins with > a certain delimiter, and ends with a certain delimiter and its length > is a multiple of three? > > I want to isolate sequences like this in a text and to work on them > only. > > Given:- > > text > text > BEGIN N N N END > text BEGIN N N N N N N END > some text BEGIN N N N N N N N N N END > text > N N N N N N N > text > > The sequences I want to work on start with BEGIN and end with END with > exact multiple of 3 B's in between with only single space. I want to > place a newline before every 3 B's. So the above text would transform > to > > text > text > BEGIN > N N N > Z > text BEGIN > N N N > N N N > Z > some text BEGIN > N N N > N N N > N N N > Z > text > N N N N N N N > text Is there a particular reason you want/need to use a (single) regular expression? It doesn't seem like a good fit to me. Unless you're somehow restricted to a single `replace-regexp', you may as well use more of Emacs's toolbox. Here's some quick-and-dirty Lisp (which, of course, uses regular expressions) that works on your example but would need more work and refinement to serve your general purpose. (defun something () (interactive) (save-excursion (goto-char (point-min)) (while (re-search-forward "BEGIN" nil t) (insert "\n") (delete-horizontal-space) (while (looking-at "\\(^\\| \\)N N N\\( \\|$\\)") (goto-char (match-end 0)) (delete-horizontal-space t) (insert "\n")) (when (looking-at "END") (replace-match "Z" nil nil nil 0))))) -- john