如何正则表达式字符串 [英] How to Regex a string
本文介绍了如何正则表达式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用正则表达式进行检查:
Is it possible to check with regex:
- 完整的字符串是数字AND
- 第一个字符是 7 或 8 然后字符串的完整长度必须是 11 OR
- 第一个字符是 1,那么字符串的完整长度必须是 10
- OR 第一个字符是 0 然后是字符串的完整长度必须是 18 AND 字符 8 必须是 8 或 7 OR 字符 9必须是 1
- the complete string are numbers AND
- the first character is a 7 or 8 then the complete length of the string must be 11 OR
- the first character is a 1 then the complete length of the string must be 10
- OR the first character is a 0 then the complete length of the string must be 18 AND on character 8 must be a 8 or 7 OR on character 9 must be a 1
我希望你能明白我的意思.希望这些例子能帮助你理解我的意思.
I hope you can see what I mean. Hope the examples will help you to know what I mean.
这是我的解决方案(无法完全工作->我不知道如何检查是否以 0 开头且长度为 18 个字符,位置 8 上的字符必须是 7 或 8 或位置 9 上的字符必须是 1):
Here is my solution(not working completely-> I don't know how to check if in case it starts with a 0 and it is 18 characters long the character on position 8 must be 7or8 or on position 9 the character must be 1):
^(?:[78]\d{10}|[1-69]\d{9}|[0]/d{18})$
例如:
- 85556987456 ->以 8 开头,长度为 11 ->匹配
- 75556987456 ->以 7 开头,长度为 11 ->匹配
- 1555698745 ->从 1 开始,长度为 10 ->匹配
- 000000085556987456 ->从 0 开始,长度为 18,在 pos 8 上是 8 ->匹配
- 000000075556987456 ->从 0 开始,长度为 18,在 pos 8 上是 7 ->匹配
- 000000001556987456 ->从 0 开始,长度为 18,在 pos 9 上是 1 ->匹配
- 85556987456 -> starts with 8 and length is 11 -> match
- 75556987456 -> starts with 7 and length is 11 -> match
- 1555698745 -> starts with 1 and length is 10 -> match
- 000000085556987456 -> starts with 0 and length is 18 and on pos 8 is a 8 -> match
- 000000075556987456 -> starts with 0 and length is 18 and on pos 8 is a 7 -> match
- 000000001556987456 -> starts with 0 and length is 18 and on pos 9 is a 1 -> match
谢谢!
推荐答案
可以使用
^(?:[78]\d{10}|1\d{9}|0\d{6}(?:[87]\d|\d1)\d{9})$
查看正则表达式演示
详情
^
- 字符串的开始(?:[78]\d{10}|1\d{9}|0\d{6}(?:[87]\d|\d1)\d{9})
- 三种选择之一:[78]\d{10}
-7
或8
然后是 10 位数字(总共 11 位)|
- 或1\d{9}
-1
然后是 9 位数字(总共 10 位)|
- 或0\d{6}(?:[87]\d|\d1)\d{9}
-0
,然后是 6 位数字,然后是第 8 位数字等于8
或7
和任意一位,或任意一位和第 9 位等于1
,然后再增加 9 位(=18 位数字)
^
- start of string(?:[78]\d{10}|1\d{9}|0\d{6}(?:[87]\d|\d1)\d{9})
- one of the three alternatives:[78]\d{10}
-7
or8
and then 10 digits (11 all in all)|
- or1\d{9}
-1
and then 9 digits (10 all in all)|
- or0\d{6}(?:[87]\d|\d1)\d{9}
-0
, then 6 digits, then the 8th digit equal to8
or7
and any one digit, or any one digit and the 9th digit equal to1
, and then 9 more digits (=18 digits)
这篇关于如何正则表达式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文