为什么匹配 1 到 10 的正则表达式写为 [1-9]|10 而不是 [1-10]? [英] Why is the regex to match 1 to 10 written as [1-9]|10 and not [1-10]?

查看:66
本文介绍了为什么匹配 1 到 10 的正则表达式写为 [1-9]|10 而不是 [1-10]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么匹配 1 到 10 数字的正则表达式通常写成这样?

Why is the regex to match numbers from 1 to 10 commonly written as follows?

[1-9]|10

代替:

[1-10]

或者这个:

[1-(10)]

推荐答案

有时候一幅价值 1000 字的好画...

以下是您问题中的三个命题以及正则表达式理解它们的方式:

Here are the three propositions in your question and the way a regex flavour would understand them:

Invalid regexp !!

此正则表达式无效,因为范围以一个数字开头 (1-) 但未以另一个数字结尾(以 ( 结尾).

This regex is invalid because a range is opened (1-) with a digit but not closed with another digit (ends with ().

一个范围通常用两边的数字或两边的字母来绑定.

A range is usually bound with digits on both sides or letters on both sides.

使用 Debuggex

这篇关于为什么匹配 1 到 10 的正则表达式写为 [1-9]|10 而不是 [1-10]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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