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.
Examples of passing: 1 12 123456 123a 1234ab 123456789ab
Examples of failing: 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#. 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
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.
/([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:
\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
Matches: 1 12 123456 123a 1234ab 123456789ab
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.