简体   繁体   English

使preg_match贪婪的正则表达式只接受某些值?

[英]Making regex for preg_match greedy to only accept certain values?

I have a CSV file that has a entry of strings that represents time/dates as follows : 我有一个CSV文件,其中包含一个表示时间/日期的字符串,如下所示:

20190816T16:10:10

I have created the following regex to preg_match but want to have the hour/mins and seconds to only allow certain numbers (eg hours would be 24 hour clock so only accept 00-23, while minutes & seconds would only accept 00-59) 我已经为preg_match创建了以下正则表达式,但希望将小时/分钟和秒设置为仅允许某些数字(例如,小时为24小时制,因此仅接受00-23,而分钟和秒仅接受00-59)

/\\d{8}[AZ]\\d{2}:\\d{2}:\\d{2}/

Can anyone suggest how to change this to ensure this regex matches the structure I am trying to achieve? 谁能建议如何更改此设置以确保此正则表达式与我要实现的结构匹配?

You need to modify your pattern to match the underlying strings making up those ranges: 您需要修改模式以匹配构成这些范围的基础字符串:

/\d{8}[A-Z](?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/

Demo 演示版

此正则表达式应符合您的结构:

\d{8}[A-Z]([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]

You can use ranges (ex: [0-9] ), with a non-capturing group for the hours (minutes & seconds are easy to match): 您可以将范围 (例如: [0-9] )与非捕获组一起使用(小时和分钟容易匹配):

//            00 to 23       00 to 59
/\d{8}[A-Z](?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM