all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nicolas Richard <theonewiththeevillook@yahoo.fr>
To: Suvayu Ali <fatkasuvayu+linux@gmail.com>
Cc: Emacs help <help-gnu-emacs@gnu.org>
Subject: Re: Question about string-match and match-string
Date: Wed, 17 Jul 2013 10:53:14 +0200	[thread overview]
Message-ID: <877ggpzhxh.fsf@yahoo.fr> (raw)
In-Reply-To: <20130717081704.GB16899@kuru.dyndns-at-home.com> (Suvayu Ali's message of "Wed, 17 Jul 2013 10:17:04 +0200")

Suvayu Ali <fatkasuvayu+linux@gmail.com> 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.



  parent reply	other threads:[~2013-07-17  8:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-17  8:17 Question about string-match and match-string Suvayu Ali
2013-07-17  8:35 ` Suvayu Ali
2013-07-17  8:44   ` Rasmus
2013-07-17  8:55     ` Suvayu Ali
2013-07-18  8:00     ` Julien Cubizolles
2013-07-18  8:33       ` Rasmus
2013-07-20 15:26         ` Julien Cubizolles
2013-07-20 22:25           ` Rasmus
2013-07-21  0:41             ` Rasmus
2013-07-21  8:15               ` Julien Cubizolles
2013-07-17  8:53 ` Nicolas Richard [this message]
2013-07-17 10:02   ` Suvayu Ali

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

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

  git send-email \
    --in-reply-to=877ggpzhxh.fsf@yahoo.fr \
    --to=theonewiththeevillook@yahoo.fr \
    --cc=fatkasuvayu+linux@gmail.com \
    --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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.