Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 修改reg exp,将4位数字或空白设置为有效_Regex - Fatal编程技术网

Regex 修改reg exp,将4位数字或空白设置为有效

Regex 修改reg exp,将4位数字或空白设置为有效,regex,Regex,我需要使用reg exp和4位数字,否则无效 我的regexp现在看起来像这样: ([01][0-9]|2[0-3])[0-5][0-9] 但我需要修改它,使它也接受空白 那么reg exp会像这样吗 ([01][0-9]|2[0-3])[0-5][0-9]?[] 只需将四位数字设置为可选 ^(?:([01][0-9]|2[0-3])[0-5][0-9])?$ 请参见上面演示链接中的空白链接。另一种解决方案: ^([0-9]{4})?(\[])?$ 演示: regexp用于验证一个字段

我需要使用reg exp和4位数字,否则无效

我的regexp现在看起来像这样:

([01][0-9]|2[0-3])[0-5][0-9]
但我需要修改它,使它也接受空白

那么reg exp会像这样吗

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

只需将四位数字设置为可选

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

请参见上面演示链接中的空白链接。

另一种解决方案:

^([0-9]{4})?(\[])?$
演示:

regexp用于验证一个字段,如果该字段包含4位数字或为空,则该字段将有效。是否为空有效?是的,如我在上一条评论中所述,空行有效。