When I use OpenResty to write Lua code, I need to use Lua regular expressions, where the pattern is like this,
--Water heater setting time
local s = '12:33'
local pattern = "(20|21|22|23|[01][0-9]):([0-5][0-9] )"
local matched = string.match(s, "(20|21|22|23|[01][0-9]):([0-5][0-9])")< br />print(matched)
Because my business requirement is to express the time in hexadecimal notation: 23:09
But I found that this pattern still can’t get what I want The matching result. This ordinary pattern will not have any problems in other programming languages, such as C, C++, python, perl, PHP, etc. Why doesn’t it work?
Later, I checked the information on the Internet and tried different patterns one by one, and found that as long as the “|” appears, it won’t work!
The following is my verification code
#!/usr/bin/lua
--Note: There is no this metacharacter in lua regular expressions
< br />local s = '12:04'
print(string.match(s, "[01][0-9]:[0-5][0-9]"))
print(string.match(s, "[01]%d:[0-5]%d"))
print(string.match(s, "[0-1]%d:[0-5 ]%d"))
print(string.match(s, "(2[0-3]|[01][0-9]):[0-5][0-9]"))
local s = '23:59'
print(string.match(s, "2[0-3]:[0-5][0-9]"))< br />print(string.match(s, "2[0-3]:[0-5]%d"))
The following is a screenshot of the output result
< img src="/wp-content/uploads/images/language/lua/1626809954055.net/2018052220153661" alt="" >
For this, I found the pits in lua regular expressions , It is different from other languages:
There is no “|” metacharacter in regular expressions< /p>
If you want to express multiple possible parallel situations, you can only write multiple patterns.
When I use OpenResty to write Lua code, I need to use Lua regular expressions, where the pattern is like this,
- -Water heater setting time
local s = '12:33'
local pattern = "(20|21|22|23|[01][0-9]):([0-5][ 0-9])"
local matched = string.match(s, "(20|21|22|23|[01][0-9]):([0-5][0-9] )")
print(matched)
Because my business requirement is to express the time in hexadecimal notation: 23:09
But I found that this pattern can never be Get the matching result I want. This ordinary pattern will not have any problems in other programming languages, such as C, C++, python, perl, PHP, etc. Why doesn’t it work?
Later, I checked the information on the Internet and tried different patterns one by one, and found that as long as the “|” appears, it won’t work!
The following is my verification code
#!/usr/bin/lua
--Note: There is no this metacharacter in lua regular expressions
< br />local s = '12:04'
print(string.match(s, "[01][0-9]:[0-5][0-9]"))
print(string.match(s, "[01]%d:[0-5]%d"))
print(string.match(s, "[0-1]%d:[0-5 ]%d"))
print(string.match(s, "(2[0-3]|[01][0-9]):[0-5][0-9]"))
local s = '23:59'
print(string.match(s, "2[0-3]:[0-5][0-9]"))< br />print(string.match(s, "2[0-3]:[0-5]%d"))
The following is a screenshot of the output result
< img src="/wp-content/uploads/images/language/lua/1626809954056.net/2018052220153661" alt="" >
For this, I found the pits in lua regular expressions , It is different from other languages:
There is no “|” metacharacter in regular expressions< /p>
If you want to express multiple possible parallel situations, you can only write multiple patterns.