all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Help improve regular expression for 24 hours format
@ 2022-11-24  3:36 Jean Louis
  2022-11-24  5:50 ` tomas
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Jean Louis @ 2022-11-24  3:36 UTC (permalink / raw)
  To: Help GNU Emacs


This regular expression shall match 24 hours format such as "12:34".

I do not like how I wrote this regular expression, is there way to
provide to `rx' function something like "[012]"?

Any other way to improve this regular expression by using rx notation
that shall match only the 24 hours format such as HH:MM where HH is
for hours and MM for minutes?

(defvar rcd-rx-time (rx (= 1 (or "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" 
 				 "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23"))
 			":" (= 1 (any "0" "1" "2" "3" "4" "5")) (= 1 (any digit)))
  "Regular expression for 24 hours format")

(string-match rcd-rx-time "23:00") ⇒ 0

--
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Help improve regular expression for 24 hours format
  2022-11-24  3:36 Help improve regular expression for 24 hours format Jean Louis
@ 2022-11-24  5:50 ` tomas
  2022-11-24  6:32 ` Yuri Khan
  2022-11-24 13:09 ` Michael Heerdegen
  2 siblings, 0 replies; 12+ messages in thread
From: tomas @ 2022-11-24  5:50 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 374 bytes --]

On Thu, Nov 24, 2022 at 06:36:34AM +0300, Jean Louis wrote:
> 
> This regular expression shall match 24 hours format such as "12:34".
> 
> I do not like how I wrote this regular expression, is there way to
> provide to `rx' function something like "[012]"?

  (any "012")

or

  (any ?0 ?1 ?2)

or

  (any "0-2")

or

  (any (?0 . ?2))

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Help improve regular expression for 24 hours format
  2022-11-24  3:36 Help improve regular expression for 24 hours format Jean Louis
  2022-11-24  5:50 ` tomas
@ 2022-11-24  6:32 ` Yuri Khan
  2022-11-24  8:59   ` Jean Louis
  2022-11-24 13:09 ` Michael Heerdegen
  2 siblings, 1 reply; 12+ messages in thread
From: Yuri Khan @ 2022-11-24  6:32 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help GNU Emacs

On Thu, 24 Nov 2022 at 10:38, Jean Louis <bugs@gnu.support> wrote:

> This regular expression shall match 24 hours format such as "12:34".
>
> I do not like how I wrote this regular expression, is there way to
> provide to `rx' function something like "[012]"?

I’d write it in classic regexp notation as

    \(?:\(?:[01][0-9]|2[0-3]\):\(?:[0-5][0-9]\)\)

and probably leave like that as it’s readable enough as is.

If you feel the need for rx here, then, as Tomas said, ‘any’ (aka ‘in’
and ‘char’) with ranges is your friend.

(Also I don’t understand your use of ‘(= 1 …)’. Looks redundant to me.)



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Help improve regular expression for 24 hours format
  2022-11-24  6:32 ` Yuri Khan
@ 2022-11-24  8:59   ` Jean Louis
  2022-11-24  9:38     ` tomas
  0 siblings, 1 reply; 12+ messages in thread
From: Jean Louis @ 2022-11-24  8:59 UTC (permalink / raw)
  To: Yuri Khan; +Cc: Help GNU Emacs

* Yuri Khan <yuri.v.khan@gmail.com> [2022-11-24 09:33]:
> On Thu, 24 Nov 2022 at 10:38, Jean Louis <bugs@gnu.support> wrote:
> 
> > This regular expression shall match 24 hours format such as "12:34".
> >
> > I do not like how I wrote this regular expression, is there way to
> > provide to `rx' function something like "[012]"?
> 
> I’d write it in classic regexp notation as
> 
>     \(?:\(?:[01][0-9]|2[0-3]\):\(?:[0-5][0-9]\)\)
> 
> and probably leave like that as it’s readable enough as is.
> 
> If you feel the need for rx here, then, as Tomas said, ‘any’ (aka ‘in’
> and ‘char’) with ranges is your friend.
> 
> (Also I don’t understand your use of ‘(= 1 …)’. Looks redundant to
> me.)

Thanks, I make it now this way:

(defvar rcd-rx-time (rx line-start (or "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" 
 				       "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23")
 			":" (any "0" "1" "2" "3" "4" "5") (any digit)
			line-end)
  "Regular expression for 24 hours format")

 ➜ "^\\(?:0[0-9]\\|1[0-9]\\|2[0-3]\\):[0-5][[:digit:]]$"

I like to have `rx' notation, and it still looks way complicated.

(string-match rcd-rx-time "00:00") ➜ 0
(string-match rcd-rx-time "30:00") ➜ nil
(string-match rcd-rx-time "24:00") ➜ nil
(string-match rcd-rx-time "23:69") ➜ nil
(string-match rcd-rx-time "23:59") ➜ 0

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Help improve regular expression for 24 hours format
  2022-11-24  8:59   ` Jean Louis
@ 2022-11-24  9:38     ` tomas
  2022-11-24 10:31       ` Jean Louis
  0 siblings, 1 reply; 12+ messages in thread
From: tomas @ 2022-11-24  9:38 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: Yuri Khan

[-- Attachment #1: Type: text/plain, Size: 1878 bytes --]

On Thu, Nov 24, 2022 at 11:59:20AM +0300, Jean Louis wrote:
> * Yuri Khan <yuri.v.khan@gmail.com> [2022-11-24 09:33]:
> > On Thu, 24 Nov 2022 at 10:38, Jean Louis <bugs@gnu.support> wrote:
> > 
> > > This regular expression shall match 24 hours format such as "12:34".
> > >
> > > I do not like how I wrote this regular expression, is there way to
> > > provide to `rx' function something like "[012]"?
> > 
> > I’d write it in classic regexp notation as
> > 
> >     \(?:\(?:[01][0-9]|2[0-3]\):\(?:[0-5][0-9]\)\)
> > 
> > and probably leave like that as it’s readable enough as is.
> > 
> > If you feel the need for rx here, then, as Tomas said, ‘any’ (aka ‘in’
> > and ‘char’) with ranges is your friend.
> > 
> > (Also I don’t understand your use of ‘(= 1 …)’. Looks redundant to
> > me.)
> 
> Thanks, I make it now this way:
> 
> (defvar rcd-rx-time (rx line-start (or "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" 
>  				       "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23")
>  			":" (any "0" "1" "2" "3" "4" "5") (any digit)
> 			line-end)
>   "Regular expression for 24 hours format")

Hm. By distributive law, (or (seq (any "01") (any digit)) (seq "2" (any "03")))
should cover your hours part. And for the minutes's first digit, remember that
`any' can do ranges: (any "0-5") resp. (any (?0 . ?5))

>  ➜ "^\\(?:0[0-9]\\|1[0-9]\\|2[0-3]\\):[0-5][[:digit:]]$"

Even taking inspiration on what the "compiler" did of your expression might
lead you to a simpler (and more readable!) expression, e.g.

  (or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3")))

although slightly longer than my first proposal (perhaps the second is
"quicker", but I'd let the regexp compiler sort that out: you are
optimising for the human reader, after all).

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Help improve regular expression for 24 hours format
  2022-11-24  9:38     ` tomas
@ 2022-11-24 10:31       ` Jean Louis
  2022-11-24 10:45         ` tomas
  0 siblings, 1 reply; 12+ messages in thread
From: Jean Louis @ 2022-11-24 10:31 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs, Yuri Khan

* tomas@tuxteam.de <tomas@tuxteam.de> [2022-11-24 12:40]:
> > Thanks, I make it now this way:
> > 
> > (defvar rcd-rx-time (rx line-start (or "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" 
> >  				       "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23")
> >  			":" (any "0" "1" "2" "3" "4" "5") (any digit)
> > 			line-end)
> >   "Regular expression for 24 hours format")
> 
> Hm. By distributive law, (or (seq (any "01") (any digit)) (seq "2" (any "03")))
> should cover your hours part. And for the minutes's first digit, remember that
> `any' can do ranges: (any "0-5") resp. (any (?0 . ?5))
> 
> >  ➜ "^\\(?:0[0-9]\\|1[0-9]\\|2[0-3]\\):[0-5][[:digit:]]$"
> 
> Even taking inspiration on what the "compiler" did of your expression might
> lead you to a simpler (and more readable!) expression, e.g.
> 
>   (or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3")))
> 
> although slightly longer than my first proposal (perhaps the second is
> "quicker", but I'd let the regexp compiler sort that out: you are
> optimising for the human reader, after all).


Do you have completed example using: 
(or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3"))) ?

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Help improve regular expression for 24 hours format
  2022-11-24 10:31       ` Jean Louis
@ 2022-11-24 10:45         ` tomas
  2022-11-24 14:22           ` [SOLVED]: " Jean Louis
  2022-11-24 14:30           ` Jean Louis
  0 siblings, 2 replies; 12+ messages in thread
From: tomas @ 2022-11-24 10:45 UTC (permalink / raw)
  To: help-gnu-emacs, Yuri Khan

[-- Attachment #1: Type: text/plain, Size: 398 bytes --]

On Thu, Nov 24, 2022 at 01:31:00PM +0300, Jean Louis wrote:

[...]

> Do you have completed example using: 
> (or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3"))) ?

I'm not supposed to be doing this now, so the rest is left as an
exercise to the reader, alas.

Hint: look at the regexp your `rx' invocation produces and translate
backwards from it.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Help improve regular expression for 24 hours format
  2022-11-24  3:36 Help improve regular expression for 24 hours format Jean Louis
  2022-11-24  5:50 ` tomas
  2022-11-24  6:32 ` Yuri Khan
@ 2022-11-24 13:09 ` Michael Heerdegen
  2 siblings, 0 replies; 12+ messages in thread
From: Michael Heerdegen @ 2022-11-24 13:09 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis <bugs@gnu.support> writes:

> This regular expression shall match 24 hours format such as "12:34".
>
> I do not like how I wrote this regular expression, is there way to
> provide to `rx' function something like "[012]"?

Suggestion to ease learning: install and ask "xr.el":

  (xr "[012]") ==> ...

Michael.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [SOLVED]: Re: Help improve regular expression for 24 hours format
  2022-11-24 10:45         ` tomas
@ 2022-11-24 14:22           ` Jean Louis
  2022-11-25 15:31             ` Emanuel Berg
  2022-11-24 14:30           ` Jean Louis
  1 sibling, 1 reply; 12+ messages in thread
From: Jean Louis @ 2022-11-24 14:22 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs, Yuri Khan

* tomas@tuxteam.de <tomas@tuxteam.de> [2022-11-24 13:47]:
> On Thu, Nov 24, 2022 at 01:31:00PM +0300, Jean Louis wrote:
> 
> [...]
> 
> > Do you have completed example using: 
> > (or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3"))) ?
> 
> I'm not supposed to be doing this now, so the rest is left as an
> exercise to the reader, alas.
> 
> Hint: look at the regexp your `rx' invocation produces and translate
> backwards from it.

I did the exercise and I came up with the below. It looks much better
this way. 🤝 Thanks much.

(defvar rcd-rx-time (rx line-start
			(or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3")))
			":"
			(seq (any "0-5") (any digit))
			line-end)
  "Regular expression for 24 hours format")

 ➜ "^\\(?:0[[:digit:]]\\|1[[:digit:]]\\|2[0-3]\\):[0-5][[:digit:]]$"

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [SOLVED]: Re: Help improve regular expression for 24 hours format
  2022-11-24 10:45         ` tomas
  2022-11-24 14:22           ` [SOLVED]: " Jean Louis
@ 2022-11-24 14:30           ` Jean Louis
  1 sibling, 0 replies; 12+ messages in thread
From: Jean Louis @ 2022-11-24 14:30 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs, Yuri Khan

All that is that I can read date and time together:

This is me special function for reading strings:

(defun rcd-ask (&optional prompt initial-input default-value auto-initial-input)
  "Modified function `read-string'.

This is shorter, simpler function that generates the prompt
automatically, generates history variable automatically and
inherits the input method. The input will be returned trimmed."
  (let* ((prompt (or prompt "Input data: "))
	 (history (rcd-ask-history-variable prompt))
	 (initial-input (cond (auto-initial-input (car (symbol-value history)))
			(initial-input initial-input)))
	 (input (read-string prompt initial-input history default-value t))
	 (input (string-trim input)))
    input))

Then I have now regular expression by which I can read time in 24
hours format:

(defvar rcd-rx-time (rx line-start
			(or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3")))
			":"
			(seq (any "0-5") (any digit))
			line-end)
  "Regular expression for 24 hours format")

Then I made simple function for that, it does not allow me to 

(defun rcd-read-24-hours-time (&optional prompt)
  (let* ((prompt (or prompt "Time (HH:MM): "))
	 (current-time (format-time-string "%H:%M"))
	 (my-time ""))
    (while (not (string-match rcd-rx-time my-time))
      (setq my-time (rcd-ask prompt current-time)))
    my-time))

(rcd-read-24-hours-time) ➜ "17:26"

Then I have this one to read the date and give result in ISO format:

(defun rcd-read-iso-date ()
  (let* ((date (calendar-read-date))
	 (month (elt date 0))
	 (day (elt date 1))
	 (year (elt date 2)))
    (format "%04d-%02d-%02d" year month day)))

(rcd-read-iso-date) ➜ "2022-11-24"

Then it comes together like this:

(defun rcd-read-date-time ()
  (let ((date (rcd-read-iso-date))
	(time (rcd-read-24-hours-time)))
    (format "%s %s" date time)))

(rcd-read-date-time) ➜ "2022-11-24 17:26"

Then I can use that type check to enter the "scheduled time" for
messages to be sent, like:                  Scheduled time   "2022-11-24 17:26:00+03"

                             ID   56174
                   Date created   "2022-11-24 09:03:40.049268"
                  Date modified   "2022-11-24 17:26:15.422416"
                   User created   "maddox"
                  User modified   "maddox"
                        Subject   nil
                   Full message   "🌅 Good morning"
                   Message type   "SMS"
                         Status   "Scheduled"
                 From people ID   nil
              From comm line ID   nil
                 From comm line   nil
                   To people ID   "Joe Doe"
                To comm line ID   nil
                   To comm line   "+xxxxxxxxxxxx"
       Related Hyperdocument #1   "My project"
       Related Hyperdocument #2   nil
       Related Hyperdocument #3   nil
                           UUID   "bbe5710d-0795-4f46-b6ce-7ae9f0f009f9"
                 Scheduled time   "2022-11-24 17:26:00+03"
                      Time sent   nil
                         Canned   nil

In the next step I have to make a timer function to verify scheduled
messages, so that they can be dispatched at specific time in future.

My subject was about the time type check, in background it is all
about human relationships.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [SOLVED]: Re: Help improve regular expression for 24 hours format
  2022-11-24 14:22           ` [SOLVED]: " Jean Louis
@ 2022-11-25 15:31             ` Emanuel Berg
  2022-11-26 18:08               ` Jean Louis
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2022-11-25 15:31 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> I did the exercise and I came up with the below. It looks
> much better this way. Thanks much.
>
> (defvar rcd-rx-time (rx line-start
> 			(or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3")))
> 			":"
> 			(seq (any "0-5") (any digit))
> 			line-end)
>   "Regular expression for 24 hours format")
>
>  -> "^\\(?:0[[:digit:]]\\|1[[:digit:]]\\|2[0-3]\\):[0-5][[:digit:]]$"

This is also something that belongs in a library.

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [SOLVED]: Re: Help improve regular expression for 24 hours format
  2022-11-25 15:31             ` Emanuel Berg
@ 2022-11-26 18:08               ` Jean Louis
  0 siblings, 0 replies; 12+ messages in thread
From: Jean Louis @ 2022-11-26 18:08 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg <incal@dataswamp.org> [2022-11-26 18:12]:
> Jean Louis wrote:
> 
> > I did the exercise and I came up with the below. It looks
> > much better this way. Thanks much.
> >
> > (defvar rcd-rx-time (rx line-start
> > 			(or (seq "0" (any digit)) (seq "1" (any digit)) (seq "2" (any "0-3")))
> > 			":"
> > 			(seq (any "0-5") (any digit))
> > 			line-end)
> >   "Regular expression for 24 hours format")
> >
> >  -> "^\\(?:0[[:digit:]]\\|1[[:digit:]]\\|2[0-3]\\):[0-5][[:digit:]]$"
> 
> This is also something that belongs in a library.

Maybe it exists already somewhere?

I use (org-calendar-select) but I missed selecting time.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-11-26 18:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-24  3:36 Help improve regular expression for 24 hours format Jean Louis
2022-11-24  5:50 ` tomas
2022-11-24  6:32 ` Yuri Khan
2022-11-24  8:59   ` Jean Louis
2022-11-24  9:38     ` tomas
2022-11-24 10:31       ` Jean Louis
2022-11-24 10:45         ` tomas
2022-11-24 14:22           ` [SOLVED]: " Jean Louis
2022-11-25 15:31             ` Emanuel Berg
2022-11-26 18:08               ` Jean Louis
2022-11-24 14:30           ` Jean Louis
2022-11-24 13:09 ` Michael Heerdegen

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.