logo elektroda
logo elektroda
X
logo elektroda

[Solved] [lua] [lua][regex] How to Match 24-Hour Format Hours (00-23) Using Regex in Lua

czasnagli 1026 9
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16983433
    czasnagli
    Level 17  
    Welcome!

    I am looking for the simplest method to check if the first two digits correspond to the 24 hour time format. At the moment I check the first two digits this way, but I would like to do it more simply. Is it possible to do the same with just a regex expression?

    Code: Lua
    Log in, to see the code
    .
    [lua] [lua][regex] How to Match 24-Hour Format Hours (00-23) Using Regex in Lua
  • ADVERTISEMENT
  • #2 16983472
    Anonymous
    Level 1  
  • #3 16983525
    czasnagli
    Level 17  
    drobok wrote:
    ([0-1][0-9]|2[0-4]):[0-5][0-9]


    In the code tester it does not want to work.
    [lua] [lua][regex] How to Match 24-Hour Format Hours (00-23) Using Regex in Lua .
  • ADVERTISEMENT
  • #4 16983585
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #5 16983707
    czasnagli
    Level 17  
    drobok wrote:
    Because note that there you are also testing if the minutes
    .
    This is also what I did in the example above, but it doesn't work.
  • Helpful post
    #6 16983853
    Anonymous
    Level 1  
  • #7 16984779
    czasnagli
    Level 17  
    drobok , now works thank you for your help.

    [lua] [lua][regex] How to Match 24-Hour Format Hours (00-23) Using Regex in Lua .
  • ADVERTISEMENT
  • #8 16987931
    JacekCz
    Level 42  
    I sympathise with 'non-regex' solutions in many situations.
    (a) firstly, sometimes the cost of regexes can't be ignored. It's fun to write, but the library has a lot of work to do. Regular expressions are popular, but 'simplest' is not always the most efficient.
    (b) secondly, often the second phase is to use the data, and sooner or later some form of parsing the data WITHIN the data comes up. E.g. whether <24 and <59. One can work oneself up to the elbows for pure sport (it is possible to write such an expression - it will be huge, who knows how to preserve it).
    Often a mini-parser is the correct solution, and many of them cost a minimal amount of CPU and very often no RAM at all. It does two things: it confirms correctness and uses the data.

    My personal version would go like this

    Code: Lua
    Log in, to see the code
    .
  • #9 16988500
    czasnagli
    Level 17  
    JacekCz wrote:
    My personal version would go like this
    .
    Thank you for the suggested solution, but in my case a regular expression would work better as the start of the hour may have a different position in the string.

    [lua] [lua][regex] How to Match 24-Hour Format Hours (00-23) Using Regex in Lua
  • #10 17794042
    czasnagli
    Level 17  
    Thank you for your help.

Topic summary

The discussion addresses how to validate the first two digits of a 24-hour time format (00-23) using Lua regular expressions. The original approach involved manual checks on each digit, but the user sought a simpler regex-based solution. A common regex pattern suggested was "([0-1][0-9]|2[0-4])", but it failed in Lua due to its limited regex support, which does not handle alternation (the "|" operator) as in POSIX regex. The recommended workaround is to use separate regex matches for ranges "[0-1][0-9]" and "2[0-3]" combined with logical OR in Lua code. Alternative suggestions included parsing substrings and comparing numeric values directly, which can be more efficient and reliable than complex regex. The user preferred regex due to variable position of the hour substring within larger strings. The final practical solution involves two separate regex checks combined programmatically to confirm valid 24-hour format hours.
Summary generated by the language model.
ADVERTISEMENT