Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Anchoring a RE in a string

37 views
Skip to first unread message

Doug Lewan

unread,
Jun 8, 2012, 2:48:52 PM6/8/12
to help-gn...@gnu.org
Currently I'm trying to check the /beginning/ of a multi-line string with a regular expression.
The scenario looks like this:

(string-match "^\\s-+ABCD"
"qwer\n zxcv\n ABCD")
^ Matches here.

I want it to fail because the beginning doesn't match.
I hadn't expected the anchor (^) to match inside the string.

I can't be the only one to have wanted to do this.
What's the right way.

Thanks.

,Douglas
Douglas Lewan
Shubert Ticketing
(201) 489-8600 ext 224



Doug Lewan

unread,
Jun 8, 2012, 3:08:24 PM6/8/12
to help-gn...@gnu.org
I've found the /adequate/ solution.

(setq s (delete ?\n s))
(string-match "^\\s-+ABCD" s)

Still, is there no way of truly anchoring at the beginning of a string?

jpkotta

unread,
Jun 8, 2012, 3:18:45 PM6/8/12
to help-gn...@gnu.org
Use \\` instead of ^. See "34.3.1.1 Special Characters in Regular Expressions" in the Elisp manual.

jpkotta

unread,
Jun 8, 2012, 3:18:45 PM6/8/12
to gnu.ema...@googlegroups.com, help-gn...@gnu.org
On Friday, June 8, 2012 2:08:24 PM UTC-5, Doug Lewan wrote:

Doug Lewan

unread,
Jun 8, 2012, 5:06:25 PM6/8/12
to help-gn...@gnu.org
\` -- empty string at the beginning of buffer

Fantastic!

In fact, I'd found my first use for \' just recently. I don't know why it didn't strike me.

I will never run out of things to learn in emacs. As a video game, I still feel like I'm on level 2.

Philipp Haselwarter

unread,
Jun 9, 2012, 3:28:22 PM6/9/12
to help-gn...@gnu.org
`rx' is cool when you're too lazy to look up special symbols


--
Philipp Haselwarter


rusi

unread,
Jun 10, 2012, 1:38:18 AM6/10/12
to
On Jun 10, 12:28 am, Philipp Haselwarter <phil...@haselwarter.org>
wrote:
> `rx' is cool when you're too lazy to look up special symbols
>
> --
> Philipp Haselwarter

Interesting!! Cant find an info page.

Philipp Haselwarter

unread,
Jun 10, 2012, 3:06:16 AM6/10/12
to help-gn...@gnu.org
Unfortunately, its only mention in emacs or elisp info is Gerd
Moellmann's authorship notice: "[...] `rx.el', a regular expression
constructor. ".

Take a look at C-h f rx RET

--
Philipp Haselwarter


Drew Adams

unread,
Jun 10, 2012, 10:34:30 AM6/10/12
to Philipp Haselwarter, help-gn...@gnu.org
> Unfortunately, its only mention in emacs or elisp info is Gerd
> Moellmann's authorship notice: "[...] `rx.el', a regular expression
> constructor. ". Take a look at C-h f rx RET

If you cannot find something in the manuals, try searching for it at EmacsWiki
(or Google after that). Entering "rx" in the wiki search field shows this page
as the first search hit: http://emacswiki.org/emacs/rx.

(That of course is not intended to mean that something should not also be
mentioned in the manuals. It's just intended to help you find information. The
wiki is another resource, like help-gn...@gnu.org.)


0 new messages