解密一个简单的正则表达式 [英] Decyphering a simple regex

查看:69
本文介绍了解密一个简单的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有问题的正则表达式是

(\d{3,4}[.-]?)+

示例文本

707-7019-789

我目前的进展

(            )+  a capturing group, capturing one or more
 \d{3,4}         digit, in quantities 3 or 4
        [.-]?    dot (or something) or hyphen, in quantities zero or one <-- this is the part I'm interested in

根据我的理解,这应该匹配 3 或 4 位数字,后跟一个点(或任何东西,因为点匹配任何东西)或一个连字符,捆绑在一个组中,一次或多次.为什么这不匹配

From my understanding this should match 3 or 4 digit number, followed by a dot (or anything, since dot matches anything) or a hyphen, bundled in a group, one or more times. Why doesn't this matches a

707+123-4567

然后呢?

推荐答案

. 在字符组中 [] 只是文字 .,它没有任何东西"的特殊含义.[.-]? 的意思是一个点或一个连字符或什么都没有",因为使用 ? 可以使整个组成为可选.

. in a character group [] is just a literal ., it does not have the special meaning "anything". [.-]? means "a dot or a hyphen or nothing", because the entire group is made optional with the ?.

这篇关于解密一个简单的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆