其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

正则表达式以特定数字开头

GG网络技术分享 2025-03-18 16:15 0


问题描述:

How to write regular expression for following conditions:

  1. Should have only numbers
  2. Must be 8 digits long
  3. Must start with 8 or 9 or 6

So for I can do only for first two conditions. I am not sure how to do the third conditions

My code is

 if (!preg_match(\'/^[0-9]{8}$/\', $number))

网友观点:

You\'re almost there. Simply remove the first number from the character class and validate it at the start of your pattern...

/^(8|9|6)\\d{7}$/

FYI - \\d is the escape sequence for digits. I suppose you could also use this

/^[896]\\d{7}$/

as it means about the same thing when you\'re only watching for a single character at the start.

Python中的正则表达式,你深入了解过吗?

正则表达式 - 简介

除非您以前使用过正则表达式,否则您可能不熟悉一些术语。但是,毫无疑问,您已经使用过不涉及脚本的某些正则表达式概念。

例如,您很可能使用 ?* 通配符来查找硬盘上的文件。? 通配符匹配文件名中的 0 个或 1 个字符,而 * 通配符匹配零个或多个字符。像 data(\\w)?\\.dat 这样的模式将查找下列文件:

标签:

提交需求或反馈

Demand feedback