From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Nicolas Richard Newsgroups: gmane.emacs.help Subject: Re: Question about string-match and match-string Date: Wed, 17 Jul 2013 10:53:14 +0200 Message-ID: <877ggpzhxh.fsf@yahoo.fr> References: <20130717081704.GB16899@kuru.dyndns-at-home.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1374051200 975 80.91.229.3 (17 Jul 2013 08:53:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 17 Jul 2013 08:53:20 +0000 (UTC) Cc: Emacs help To: Suvayu Ali Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jul 17 10:53:20 2013 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 1UzNU7-0005ST-Df for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Jul 2013 10:53:19 +0200 Original-Received: from localhost ([::1]:52028 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzNU6-0000Xw-Sq for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Jul 2013 04:53:18 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43832) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzNTr-0000Xa-8W for help-gnu-emacs@gnu.org; Wed, 17 Jul 2013 04:53:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UzNTq-0007EX-57 for help-gnu-emacs@gnu.org; Wed, 17 Jul 2013 04:53:03 -0400 Original-Received: from mxin.ulb.ac.be ([164.15.128.112]:53996) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzNTp-0007E0-VR for help-gnu-emacs@gnu.org; Wed, 17 Jul 2013 04:53:02 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap8EAHBZ5lGkD4Nx/2dsb2JhbABaxkKBJHSCIwEBBAF5BQsIAyElDwEEDTwTh30BAwkGpFeHNwFKDYhejQSCageDegOVc4FphhCGF4UmgxQ6 Original-Received: from geodiff-mac3.ulb.ac.be (HELO geodiff-mac3) ([164.15.131.113]) by smtp.ulb.ac.be with ESMTP; 17 Jul 2013 10:53:00 +0200 In-Reply-To: <20130717081704.GB16899@kuru.dyndns-at-home.com> (Suvayu Ali's message of "Wed, 17 Jul 2013 10:17:04 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 164.15.128.112 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:92198 Archived-At: Suvayu Ali writes: > I was writing a filter for Org mode and came across this confusion. I > want to parse a string with LaTeX code and delete matching > sub-expressions. As a test I tried this: > > (let ((test "\\section{Heading{ignoreheading}}\nText\n")) > (string-match "\\(\\\\\\\\section{.+{ignoreheading}}\\\\n\\)\\(.+\\)" test) > (match-substitute-replacement "" t nil test 1) > ) Easy : the string doesn't match. (let ((test "\\section{Heading{ignoreheading}}\nText\n")) (if (string-match "\\(\\\\\\\\section{.+{ignoreheading}}\\\\n\\)\\(.+\\)" test) (match-substitute-replacement "" t nil test 1) "Error")) => "Error" What you want is probably : (let ((test "\\section{Heading{ignoreheading}}\nText\n")) (if (string-match "\\(\\\\section{.+{ignoreheading}}\n\\)\\(.+\\)" test) (match-substitute-replacement "" t nil test 1) "Error")) Only \\\\ before "section", because elisp translates that into \\, which is a regexp matching exactly one backslash char (and that's all there is in your string, since \\ is translated to a single backslash). \\\\n became \n, because the former would translate into the regexp \\n, which matches a backslash followed by the character 'n', and you don't have that in your string. Note that the dot doesn't match newlines by default, so your group will not contain the final newline. As a general rule, you should probably check if (string-match) worked before using the match data ; otherwise it leads to unhelpful error messages. > PS: I'm also not very clear why we need 4 \-s in the regex to match a > single \ in the string. I came up with it by trial and error with the > regexp-builder. Any explanation about that would also be great. I hope the above is an explanation of that. Just remember there are two steps: (i) emacs reads the string (ii) the string is made into a regexp so for matching on \, you need the regexp \\, which is obtained by the string "\\\\" (e.g. (insert "\\\\") inserts \\) -- Nico.