[英]Need a regex for numbers followed by letters no spaces, dashes, slashes, etc
I'm just not that good at regex and I haven't been able to find an example of something close to what I need. 我只是不擅长正则表达式,所以我找不到能够满足我需求的示例。 Thanks in advance for your help.
在此先感谢您的帮助。 This is for a search entry box that will take the user directly to what they are looking for if they already know it's number or number+letter sequence
这是一个搜索输入框,如果用户已经知道它是数字或数字+字母序列,它将直接将用户带到他们要查找的内容
It has to start with a number and the number can be 1 to many digits, the letters following the numbers can be 0, 1, or 2 digits. 它必须以数字开头,并且数字可以是1到许多数字,数字后面的字母可以是0、1或2位数字。
Examples of passing: 1 12 123456 123a 1234ab 123456789ab 通过示例:1 12 123456 123a 1234ab 123456789ab
Examples of failing: a ab a1 ab12 1abc 123abc 1-a 1_a 失败的示例:ab a1 ab12 1abc 123abc 1-a 1_a
you get the point. 你明白了。 For the passing strings, I also need to separate the numbers from the letters.
对于传递的字符串,我还需要将数字和字母分开。
Thanks again. 再次感谢。
Language is c#. 语言是c#。 I need to test the incoming string and then split it into it's number and letter parts if it passes the regex.
我需要测试传入的字符串,然后将其通过正则表达式分成数字和字母部分。
try 尝试
[0-9]+[a-z]{0,2}
Hope this helps. 希望这可以帮助。
Try this 尝试这个
var regex=new Regex("^(?<numbers>[0-9]+)(?<letters>[a-z]{0,2})$",RegexOptions.IgnoreCase);
var match=regex.Match(testString);
The property match.Success
tells you if it succeed and the values can be obtained 属性
match.Success
告诉您是否成功,并且可以获取值
var numbers=match.groups["numbers"].Value;
var letters=match.groups["letters"].Value;
This should work: 这应该工作:
/([0-9]+)([a-z]{0,2})/
Depending upon what language you're using this regex in, the method for getting the full pattern match and subsequent subpattern matches may vary. 根据您使用此正则表达式的语言,获取完全模式匹配和后续子模式匹配的方法可能会有所不同。
EDIT: 编辑:
Revised second subpattern to match no letters, or a maximum of 2 letters. 修改了第二个子模式,以匹配任何字母,或最多2个字母。
/([0-9]+)([a-zA-Z]{0,2})/
大写...
All the answers given still match the 1-a in the failing list, so here's my addition: 给出的所有答案仍与失败列表中的1-a匹配,所以这是我的补充:
\b([0-9]+)([a-z]{0,2})(?!-)\b
Tested with: 1 12 123456 123a 1234ab 123456789ab a ab a1 ab12 1abc 123abc 1-a 1_a 经过测试:1 12 123456 123a 1234ab 123456789ab a ab a1 ab12 1abc 123abc 1-a 1_a
Matches: 1 12 123456 123a 1234ab 123456789ab 匹配项:1 12 123456 123a 1234ab 123456789ab
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.