help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Matching labels with buttons


From: Stephen Berman
Subject: Re: Matching labels with buttons
Date: Mon, 15 Jul 2024 14:52:24 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

On Mon, 15 Jul 2024 12:35:53 +0000 Heime <heimeborgia@protonmail.com> wrote:

> On Monday, July 15th, 2024 at 11:56 PM, Stephen Berman
> <stephen.berman@gmx.net> wrote:
>
>> On Mon, 15 Jul 2024 11:25:28 +0000 Heime heimeborgia@protonmail.com wrote:
>>
>> > Sent with Proton Mail secure email.
>> >
>> > On Monday, July 15th, 2024 at 11:17 PM, Heime heimeborgia@protonmail.com 
>> > wrote:
>> >
>> > > I want to match cases such as
>> > >
>> > > Label [-]
>> > >
>> > > Have constructed the following regexp, but it does not match the above
>> > >
>> > > "\\(.\\)\\(\\s-\\[\\-\\]\\s-\\)"
>> > >
>> > > I aw using
>> > >
>> > > (when (string-match "\\(.\\)\\(\\s-\\[\\-\\]\\s-\\)" text)
>> >
>> > In the code I have
>> >
>> > (if (string-match "\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" label) ; [-] LB
>> >
>> > (progn
>> > (setq bt (match-string 1 label))
>> > (setq lb (match-string 2 label))
>> > (setq result
>> > (concat bt (propertize lb 'face '(:foreground "red")))))
>> >
>> > (when (string-match "\\(.\\)\\(\\s-\\[\\-\\]\\s-*\\)" label) ; LB [-]
>> >
>> > (setq lb (match-string 1 label))
>> > (setq bt (match-string 2 label))
>> > (setq result
>> > (concat (propertize lb 'face '(:foreground "red")) bt)))
>> >
>> > Doing some tests with
>> >
>> > "OFF [-]"
>> >
>> > keeps matching the first string-match
>>
>>
>> That's because your regexp isn't anchored, so string-match succeeds if
>> it finds a match anywhere in the string passed to it. To avoid this,
>> start the regexp with "\\`", which anchors it to the start of the string
>> being matched against; see (info "(elisp) Regexp Backslash").
>>
>> Steve Berman
>
>
> I would like to have two regex expressions, one to match only " [-] LABEL " 
> and
> another to match only " LABEL [-] ".  With any number of whitespace.
>
> Can one use "^" ?  Or is "\\`" preferred ?

If you are always matching against a string, e.g. just using
string-match, then IIUC "^" and "\\`" give the same results.  If you are
matching against test in a buffer, e.g. with re-search-forward,
looking-at etc., then they can differ: "^" matches the beginning the the
line containing the matched string, "\\`" the beginning of the string
itself, regardless of where in the line it is (at point-min the results
are the same).

>                                            And can one use "[[:space:]]*" 
> rather
> than "\\s-*" ?  Which is preferred ?

IIUC these both give the same results.

Steve Berman



reply via email to

[Prev in Thread] Current Thread [Next in Thread]