* Matching labels with buttons @ 2024-07-15 11:17 Heime 2024-07-15 11:25 ` Heime 0 siblings, 1 reply; 17+ messages in thread From: Heime @ 2024-07-15 11:17 UTC (permalink / raw) To: Heime via Users list for the GNU Emacs text editor 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) Sent with Proton Mail secure email. ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 11:17 Matching labels with buttons Heime @ 2024-07-15 11:25 ` Heime 2024-07-15 11:56 ` Stephen Berman 2024-07-15 11:57 ` Bruno Barbier 0 siblings, 2 replies; 17+ messages in thread From: Heime @ 2024-07-15 11:25 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor 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 ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 11:25 ` Heime @ 2024-07-15 11:56 ` Stephen Berman 2024-07-15 12:35 ` Heime 2024-07-15 11:57 ` Bruno Barbier 1 sibling, 1 reply; 17+ messages in thread From: Stephen Berman @ 2024-07-15 11:56 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor 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 ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 11:56 ` Stephen Berman @ 2024-07-15 12:35 ` Heime 2024-07-15 12:52 ` Stephen Berman 0 siblings, 1 reply; 17+ messages in thread From: Heime @ 2024-07-15 12:35 UTC (permalink / raw) To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor 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 ? And can one use "[[:space:]]*" rather than "\\s-*" ? Which is preferred ? ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 12:35 ` Heime @ 2024-07-15 12:52 ` Stephen Berman 2024-07-15 12:58 ` Heime 0 siblings, 1 reply; 17+ messages in thread From: Stephen Berman @ 2024-07-15 12:52 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor 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 ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 12:52 ` Stephen Berman @ 2024-07-15 12:58 ` Heime 2024-07-15 13:42 ` Stephen Berman 0 siblings, 1 reply; 17+ messages in thread From: Heime @ 2024-07-15 12:58 UTC (permalink / raw) To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor Sent with Proton Mail secure email. On Tuesday, July 16th, 2024 at 12:52 AM, Stephen Berman <stephen.berman@gmx.net> wrote: > 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 Would I introduce \\` before the first grouping "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" How would I print \\` in a docstring ? ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 12:58 ` Heime @ 2024-07-15 13:42 ` Stephen Berman 2024-07-15 16:24 ` Heime 0 siblings, 1 reply; 17+ messages in thread From: Stephen Berman @ 2024-07-15 13:42 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor On Mon, 15 Jul 2024 12:58:05 +0000 Heime <heimeborgia@protonmail.com> wrote: > Sent with Proton Mail secure email. > > On Tuesday, July 16th, 2024 at 12:52 AM, Stephen Berman > <stephen.berman@gmx.net> wrote: > >> 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 > > Would I introduce \\` before the first grouping > > "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" Since "\\`" matches the empty string, I don't think it matters whether it's inside or outside of the group. But since in this case it's anchoring the entire regexp, it seems conceptually preferable to keep it outside (in contrast, e.g., to the case where the regexp is a disjunction and you only want to anchor one of the disjuncts). > How would I print \\` in a docstring ? You have to escape the "`": \\\\=` (not \\=\\`). Steve Berman ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 13:42 ` Stephen Berman @ 2024-07-15 16:24 ` Heime 2024-07-15 17:25 ` Heime 0 siblings, 1 reply; 17+ messages in thread From: Heime @ 2024-07-15 16:24 UTC (permalink / raw) To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor On Tuesday, July 16th, 2024 at 1:42 AM, Stephen Berman <stephen.berman@gmx.net> wrote: > On Mon, 15 Jul 2024 12:58:05 +0000 Heime heimeborgia@protonmail.com wrote: > > > Sent with Proton Mail secure email. > > > > On Tuesday, July 16th, 2024 at 12:52 AM, Stephen Berman > > stephen.berman@gmx.net wrote: > > > > > 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 > > > > Would I introduce \\` before the first grouping > > > > "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" > > > Since "\\`" matches the empty string, I don't think it matters whether > it's inside or outside of the group. But since in this case it's > anchoring the entire regexp, it seems conceptually preferable to keep it > outside (in contrast, e.g., to the case where the regexp is a > disjunction and you only want to anchor one of the disjuncts). > > > How would I print \\` in a docstring ? > > > You have to escape the "`": \\\\\\\\=` (not \\=\\`). > > Steve Berman I also have to handle the case of " LABEL [-] " with a different regex, so I can distinguish between " [-] LABEL " and " LABEL [-] ". ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 16:24 ` Heime @ 2024-07-15 17:25 ` Heime 2024-07-15 21:20 ` Stephen Berman 0 siblings, 1 reply; 17+ messages in thread From: Heime @ 2024-07-15 17:25 UTC (permalink / raw) To: Heime; +Cc: Stephen Berman, Heime via Users list for the GNU Emacs text editor On Tuesday, July 16th, 2024 at 4:24 AM, Heime <heimeborgia@protonmail.com> wrote: > On Tuesday, July 16th, 2024 at 1:42 AM, Stephen Berman stephen.berman@gmx.net wrote: > > > On Mon, 15 Jul 2024 12:58:05 +0000 Heime heimeborgia@protonmail.com wrote: > > > > > Sent with Proton Mail secure email. > > > > > > On Tuesday, July 16th, 2024 at 12:52 AM, Stephen Berman > > > stephen.berman@gmx.net wrote: > > > > > > > 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 > > > > > > Would I introduce \\` before the first grouping > > > > > > "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" > > > > Since "\\`" matches the empty string, I don't think it matters whether > > it's inside or outside of the group. But since in this case it's > > anchoring the entire regexp, it seems conceptually preferable to keep it > > outside (in contrast, e.g., to the case where the regexp is a > > disjunction and you only want to anchor one of the disjuncts). > > > > > How would I print \\` in a docstring ? > > > > You have to escape the "`": \\\\\\\\\\\\\\\\=` (not \\=\\`). > > > > Steve Berman > > I also have to handle the case of " LABEL [-] " with a different regex, so I can > distinguish between " [-] LABEL " and " LABEL [-] ". In the latter case I want to match [-] at the end with any trailing spaces. ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 17:25 ` Heime @ 2024-07-15 21:20 ` Stephen Berman 2024-07-15 21:29 ` Heime 0 siblings, 1 reply; 17+ messages in thread From: Stephen Berman @ 2024-07-15 21:20 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor On Mon, 15 Jul 2024 17:25:42 +0000 Heime <heimeborgia@protonmail.com> wrote: > On Tuesday, July 16th, 2024 at 4:24 AM, Heime <heimeborgia@protonmail.com> wrote: > >> On Tuesday, July 16th, 2024 at 1:42 AM, Stephen Berman >> stephen.berman@gmx.net wrote: >> >> > On Mon, 15 Jul 2024 12:58:05 +0000 Heime heimeborgia@protonmail.com wrote: >> > >> > > Sent with Proton Mail secure email. >> > > >> > > On Tuesday, July 16th, 2024 at 12:52 AM, Stephen Berman >> > > stephen.berman@gmx.net wrote: >> > > >> > > > 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 >> > > >> > > Would I introduce \\` before the first grouping >> > > >> > > "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" >> > >> > Since "\\`" matches the empty string, I don't think it matters whether >> > it's inside or outside of the group. But since in this case it's >> > anchoring the entire regexp, it seems conceptually preferable to keep it >> > outside (in contrast, e.g., to the case where the regexp is a >> > disjunction and you only want to anchor one of the disjuncts). >> > >> > > How would I print \\` in a docstring ? >> > >> > You have to escape the "`": \\\\\\\\\\\\\\\\=` (not \\=\\`). >> > >> > Steve Berman >> >> I also have to handle the case of " LABEL [-] " with a different regex, so I can >> distinguish between " [-] LABEL " and " LABEL [-] ". > > In the latter case I want to match [-] at the end with any trailing spaces. With your original code amended by anchoring the first regexp as I suggested, I think it handles both cases you want; at least the brief tests I tried worked. If you don't get the results you want, please show the complete code you're using and examples where it fails. Steve Berman ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 21:20 ` Stephen Berman @ 2024-07-15 21:29 ` Heime 2024-07-15 21:50 ` Stephen Berman 0 siblings, 1 reply; 17+ messages in thread From: Heime @ 2024-07-15 21:29 UTC (permalink / raw) To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor On Tuesday, July 16th, 2024 at 9:20 AM, Stephen Berman <stephen.berman@gmx.net> wrote: > On Mon, 15 Jul 2024 17:25:42 +0000 Heime heimeborgia@protonmail.com wrote: > > > On Tuesday, July 16th, 2024 at 4:24 AM, Heime heimeborgia@protonmail.com wrote: > > > > > On Tuesday, July 16th, 2024 at 1:42 AM, Stephen Berman > > > stephen.berman@gmx.net wrote: > > > > > > > On Mon, 15 Jul 2024 12:58:05 +0000 Heime heimeborgia@protonmail.com wrote: > > > > > > > > > Sent with Proton Mail secure email. > > > > > > > > > > On Tuesday, July 16th, 2024 at 12:52 AM, Stephen Berman > > > > > stephen.berman@gmx.net wrote: > > > > > > > > > > > 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 > > > > > > > > > > Would I introduce \\` before the first grouping > > > > > > > > > > "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" > > > > > > > > Since "\\`" matches the empty string, I don't think it matters whether > > > > it's inside or outside of the group. But since in this case it's > > > > anchoring the entire regexp, it seems conceptually preferable to keep it > > > > outside (in contrast, e.g., to the case where the regexp is a > > > > disjunction and you only want to anchor one of the disjuncts). > > > > > > > > > How would I print \\` in a docstring ? > > > > > > > > You have to escape the "`": \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\=` (not \\=\\`). > > > > > > > > Steve Berman > > > > > > I also have to handle the case of " LABEL [-] " with a different regex, so I can > > > distinguish between " [-] LABEL " and " LABEL [-] ". > > > > In the latter case I want to match [-] at the end with any trailing spaces. > > > With your original code amended by anchoring the first regexp as I > suggested, I think it handles both cases you want; at least the brief > tests I tried worked. If you don't get the results you want, please > show the complete code you're using and examples where it fails. > > Steve Berman Have used "\\`\\(\\s-*\\[\\-\\]\\s-*\\)\\(.*\\)" for " [-] LABEL " and "\\(.*\\)\\(\\s-*\\[\\-\\]\\s-*\\)\\'" for " LABEL [-] " ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 21:29 ` Heime @ 2024-07-15 21:50 ` Stephen Berman 2024-07-15 22:29 ` Heime 0 siblings, 1 reply; 17+ messages in thread From: Stephen Berman @ 2024-07-15 21:50 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor On Mon, 15 Jul 2024 21:29:38 +0000 Heime <heimeborgia@protonmail.com> wrote: > On Tuesday, July 16th, 2024 at 9:20 AM, Stephen Berman > <stephen.berman@gmx.net> wrote: > >> On Mon, 15 Jul 2024 17:25:42 +0000 Heime heimeborgia@protonmail.com wrote: >> >> > On Tuesday, July 16th, 2024 at 4:24 AM, Heime heimeborgia@protonmail.com wrote: [...] >> > > I also have to handle the case of " LABEL [-] " with a different regex, >> > > so I can >> > > distinguish between " [-] LABEL " and " LABEL [-] ". >> > >> > In the latter case I want to match [-] at the end with any trailing spaces. >> >> >> With your original code amended by anchoring the first regexp as I >> suggested, I think it handles both cases you want; at least the brief >> tests I tried worked. If you don't get the results you want, please >> show the complete code you're using and examples where it fails. >> >> Steve Berman > > Have used > > "\\`\\(\\s-*\\[\\-\\]\\s-*\\)\\(.*\\)" for " [-] LABEL " > > and > > "\\(.*\\)\\(\\s-*\\[\\-\\]\\s-*\\)\\'" for " LABEL [-] " Since you didn't do what I requested, I've done it. Evaluate the following code, which is based on your original code amended as I suggested (I didn't use "\\'" in the second regexp, but the results are the same with and without it): (defun heime-button (label) (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)))) (insert result)) (with-current-buffer (get-buffer-create "*Heime Test*") (erase-buffer) (heime-button " [-] LABEL ") (newline) (heime-button " LABEL [-] ")) (switch-to-buffer "*Heime Test*") What I see in buffer *Heime Test* is this: [-] LABEL LABEL [-] where each string "LABEL" is red. Is this not what you want? Steve Berman ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 21:50 ` Stephen Berman @ 2024-07-15 22:29 ` Heime 2024-07-15 22:46 ` Stephen Berman 0 siblings, 1 reply; 17+ messages in thread From: Heime @ 2024-07-15 22:29 UTC (permalink / raw) To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor On Tuesday, July 16th, 2024 at 9:50 AM, Stephen Berman <stephen.berman@gmx.net> wrote: > On Mon, 15 Jul 2024 21:29:38 +0000 Heime heimeborgia@protonmail.com wrote: > > > On Tuesday, July 16th, 2024 at 9:20 AM, Stephen Berman > > stephen.berman@gmx.net wrote: > > > > > On Mon, 15 Jul 2024 17:25:42 +0000 Heime heimeborgia@protonmail.com wrote: > > > > > > > On Tuesday, July 16th, 2024 at 4:24 AM, Heime heimeborgia@protonmail.com wrote: > > [...] > > > > > > I also have to handle the case of " LABEL [-] " with a different regex, > > > > > so I can > > > > > distinguish between " [-] LABEL " and " LABEL [-] ". > > > > > > > > In the latter case I want to match [-] at the end with any trailing spaces. > > > > > > With your original code amended by anchoring the first regexp as I > > > suggested, I think it handles both cases you want; at least the brief > > > tests I tried worked. If you don't get the results you want, please > > > show the complete code you're using and examples where it fails. > > > > > > Steve Berman > > > > Have used > > > > "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" for " [-] LABEL " > > > > and > > > > "\\(.\\)\\(\\s-\\[\\-\\]\\s-*\\)\\'" for " LABEL [-] " > > > Since you didn't do what I requested, I've done it. Evaluate the > following code, which is based on your original code amended as I > suggested (I didn't use "\\'" in the second regexp, but the results are > the same with and without it): Was that about showing the code ? It is basically writing down the regex strings. > (defun heime-button (label) > (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)))) > (insert result)) > > (with-current-buffer (get-buffer-create "Heime Test") > (erase-buffer) > (heime-button " [-] LABEL ") > (newline) > (heime-button " LABEL [-] ")) > > (switch-to-buffer "Heime Test") > > What I see in buffer Heime Test is this: > > [-] LABEL > LABEL [-] > > where each string "LABEL" is red. Is this not what you want? > > Steve Berman Yes it is what I wanted. Having read the documentation, why don't you apply \\' for the second case to match the empty string, but only at the end of string being matched against ? ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 22:29 ` Heime @ 2024-07-15 22:46 ` Stephen Berman 2024-07-15 23:02 ` Heime 0 siblings, 1 reply; 17+ messages in thread From: Stephen Berman @ 2024-07-15 22:46 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor On Mon, 15 Jul 2024 22:29:27 +0000 Heime <heimeborgia@protonmail.com> wrote: > On Tuesday, July 16th, 2024 at 9:50 AM, Stephen Berman > <stephen.berman@gmx.net> wrote: > >> On Mon, 15 Jul 2024 21:29:38 +0000 Heime heimeborgia@protonmail.com wrote: >> >> > On Tuesday, July 16th, 2024 at 9:20 AM, Stephen Berman >> > stephen.berman@gmx.net wrote: >> > >> > > On Mon, 15 Jul 2024 17:25:42 +0000 Heime heimeborgia@protonmail.com wrote: >> > > >> > > > On Tuesday, July 16th, 2024 at 4:24 AM, Heime >> > > > heimeborgia@protonmail.com wrote: >> >> [...] >> >> > > > > I also have to handle the case of " LABEL [-] " with a different regex, >> > > > > so I can >> > > > > distinguish between " [-] LABEL " and " LABEL [-] ". >> > > > >> > > > In the latter case I want to match [-] at the end with any trailing spaces. >> > > >> > > With your original code amended by anchoring the first regexp as I >> > > suggested, I think it handles both cases you want; at least the brief >> > > tests I tried worked. If you don't get the results you want, please >> > > show the complete code you're using and examples where it fails. >> > > >> > > Steve Berman >> > >> > Have used >> > >> > "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" for " [-] LABEL " >> > >> > and >> > >> > "\\(.\\)\\(\\s-\\[\\-\\]\\s-*\\)\\'" for " LABEL [-] " >> >> >> Since you didn't do what I requested, I've done it. Evaluate the >> following code, which is based on your original code amended as I >> suggested (I didn't use "\\'" in the second regexp, but the results are >> the same with and without it): > > Was that about showing the code ? It is basically writing down the regex strings. I asked for the complete code, so I don't have to guess what you're doing. >> (defun heime-button (label) >> (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)))) >> (insert result)) >> >> (with-current-buffer (get-buffer-create "Heime Test") >> (erase-buffer) >> (heime-button " [-] LABEL ") >> (newline) >> (heime-button " LABEL [-] ")) >> >> (switch-to-buffer "Heime Test") >> >> What I see in buffer Heime Test is this: >> >> [-] LABEL >> LABEL [-] >> >> where each string "LABEL" is red. Is this not what you want? >> >> Steve Berman > > Yes it is what I wanted. Having read the documentation, why don't you apply > \\' for the second case to match the empty string, but only at the end of string > being matched against ? As I wrote, for the examples you've given, the results are the same with and without "\\'" at the end of the second regexp, so omitting it simplifies the regexp. But if you want to avoid matching a string like " LABEL [-] some other text", then you should include the end anchor "\\'". Steve Berman ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 22:46 ` Stephen Berman @ 2024-07-15 23:02 ` Heime 0 siblings, 0 replies; 17+ messages in thread From: Heime @ 2024-07-15 23:02 UTC (permalink / raw) To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor On Tuesday, July 16th, 2024 at 10:46 AM, Stephen Berman <stephen.berman@gmx.net> wrote: > On Mon, 15 Jul 2024 22:29:27 +0000 Heime heimeborgia@protonmail.com wrote: > > > On Tuesday, July 16th, 2024 at 9:50 AM, Stephen Berman > > stephen.berman@gmx.net wrote: > > > > > On Mon, 15 Jul 2024 21:29:38 +0000 Heime heimeborgia@protonmail.com wrote: > > > > > > > On Tuesday, July 16th, 2024 at 9:20 AM, Stephen Berman > > > > stephen.berman@gmx.net wrote: > > > > > > > > > On Mon, 15 Jul 2024 17:25:42 +0000 Heime heimeborgia@protonmail.com wrote: > > > > > > > > > > > On Tuesday, July 16th, 2024 at 4:24 AM, Heime > > > > > > heimeborgia@protonmail.com wrote: > > > > > > [...] > > > > > > > > > > I also have to handle the case of " LABEL [-] " with a different regex, > > > > > > > so I can > > > > > > > distinguish between " [-] LABEL " and " LABEL [-] ". > > > > > > > > > > > > In the latter case I want to match [-] at the end with any trailing spaces. > > > > > > > > > > With your original code amended by anchoring the first regexp as I > > > > > suggested, I think it handles both cases you want; at least the brief > > > > > tests I tried worked. If you don't get the results you want, please > > > > > show the complete code you're using and examples where it fails. > > > > > > > > > > Steve Berman > > > > > > > > Have used > > > > > > > > "\\`\\(\\s-\\[\\-\\]\\s-\\)\\(.*\\)" for " [-] LABEL " > > > > > > > > and > > > > > > > > "\\(.\\)\\(\\s-\\[\\-\\]\\s-*\\)\\'" for " LABEL [-] " > > > > > > Since you didn't do what I requested, I've done it. Evaluate the > > > following code, which is based on your original code amended as I > > > suggested (I didn't use "\\'" in the second regexp, but the results are > > > the same with and without it): > > > > Was that about showing the code ? It is basically writing down the regex strings. > > > I asked for the complete code, so I don't have to guess what you're doing. > > > > (defun heime-button (label) > > > (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)))) > > > (insert result)) > > > > > > (with-current-buffer (get-buffer-create "Heime Test") > > > (erase-buffer) > > > (heime-button " [-] LABEL ") > > > (newline) > > > (heime-button " LABEL [-] ")) > > > > > > (switch-to-buffer "Heime Test") > > > > > > What I see in buffer Heime Test is this: > > > > > > [-] LABEL > > > LABEL [-] > > > > > > where each string "LABEL" is red. Is this not what you want? > > > > > > Steve Berman > > > > Yes it is what I wanted. Having read the documentation, why don't you apply > > \\' for the second case to match the empty string, but only at the end of string > > being matched against ? > > > As I wrote, for the examples you've given, the results are the same with > and without "\\'" at the end of the second regexp, so omitting it > simplifies the regexp. But if you want to avoid matching a string like > " LABEL [-] some other text", then you should include the end anchor "\\'". > > Steve Berman Yes, I want to avoid matching " LABEL [-] some other text". ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 11:25 ` Heime 2024-07-15 11:56 ` Stephen Berman @ 2024-07-15 11:57 ` Bruno Barbier 2024-07-15 18:29 ` tpeplt 1 sibling, 1 reply; 17+ messages in thread From: Bruno Barbier @ 2024-07-15 11:57 UTC (permalink / raw) To: Heime, Heime; +Cc: Heime via Users list for the GNU Emacs text editor Heime <heimeborgia@protonmail.com> writes: > 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 Because it does match: (string-match "\\(\\s-*\\[\\-\\]\\s-*\\)\\(.*\\)" "OFF [-]") => 3 When building regular expressions, I'm often using the amazing M-x re-builder: it allows to construct regexps interactively with visual feedbacks, it even understands the "rx" syntax, for more readable regexps. HTH, Bruno ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Matching labels with buttons 2024-07-15 11:57 ` Bruno Barbier @ 2024-07-15 18:29 ` tpeplt 0 siblings, 0 replies; 17+ messages in thread From: tpeplt @ 2024-07-15 18:29 UTC (permalink / raw) To: Bruno Barbier; +Cc: Heime, Heime via Users list for the GNU Emacs text editor Bruno Barbier <brubar.cs@gmail.com> writes: > Heime <heimeborgia@protonmail.com> writes: > > ... > > Because it does match: > > (string-match "\\(\\s-*\\[\\-\\]\\s-*\\)\\(.*\\)" "OFF [-]") > => 3 > > When building regular expressions, I'm often using the amazing M-x > re-builder: it allows to construct regexps interactively with visual > feedbacks, it even understands the "rx" syntax, for more readable > regexps. > Seconded. Build your regular expression using the regular-expression builder, ‘re-builder’. See (info "(elisp) Regular Expressions"). Note that ‘re-builder’ comes with a menu that you can use either to carry out re-builder commands or to learn the re-builder key sequences for commands. An approach you can try: 1. Create a text file (or a only a buffer) that contains examples of strings that you want your regular expression to match. Start with the simplest example and build up line-by-line with additional examples to the most complex example that you can think of. 2. Start re-builder with M-x re-builder. Enter the simplest regular expression that should match your simplest example. Read the Elisp manual entry above, if necessary, to resolve any misunderstanding if your regular expression does not match. Once the simplest regular expression matches your simplest example, add to your regular expression in order to match the second example. The new expression should match your first and second example before you move to matching your third example. And so on. -- The lyf so short, the craft so long to lerne. - Geoffrey Chaucer, The Parliament of Birds. ^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2024-07-15 23:02 UTC | newest] Thread overview: 17+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-07-15 11:17 Matching labels with buttons Heime 2024-07-15 11:25 ` Heime 2024-07-15 11:56 ` Stephen Berman 2024-07-15 12:35 ` Heime 2024-07-15 12:52 ` Stephen Berman 2024-07-15 12:58 ` Heime 2024-07-15 13:42 ` Stephen Berman 2024-07-15 16:24 ` Heime 2024-07-15 17:25 ` Heime 2024-07-15 21:20 ` Stephen Berman 2024-07-15 21:29 ` Heime 2024-07-15 21:50 ` Stephen Berman 2024-07-15 22:29 ` Heime 2024-07-15 22:46 ` Stephen Berman 2024-07-15 23:02 ` Heime 2024-07-15 11:57 ` Bruno Barbier 2024-07-15 18:29 ` tpeplt
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).