我想使用正则表达式匹配数字和字符串模式 [英] i want to match a digits and string pattern using regex

查看:462
本文介绍了我想使用正则表达式匹配数字和字符串模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1 - 前n位数只有一个连字符最后n个字母数字值

2 - 只包含一个连字符没有其他特殊字符

例如:123- abc - 正确的模式

123456-abcdgdt - 正确的模式

-adadad - 错误的模式

123 - adad - 错误的模式

-123-adad - 错误模式

abcd-123-ada - 错误模式

1-First n number of digits only then a hyphen last n number of alphanumeric values
2-contains only one hyphen no other special characters
eg : 123-abc - right pattern
123456-abcdgdt - right pattern
-adadad - wrong pattern
123--adad - wrong pattern
-123-adad - wrong pattern
abcd-123-ada - wrong pattern

推荐答案

^ [0-9] + [ - ] {1} [a-zA-Z] +
^[0-9]+[-]{1}[a-zA-Z]+






^字符串的开头

[0-9] +只接受数字1或更多

[ - ] {1}只需一次破折

[a-zA-Z] +只接受1个或更多的字母


^ start of the string
[0-9]+ accept only numbers 1 or more
[-]{1} take exactly one dash
[a-zA-Z]+ accept only letters 1 or more


字符串的结尾


这篇关于我想使用正则表达式匹配数字和字符串模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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