简体   繁体   中英

Regex for specific string

The string should look like this

"1-7;IN;dd"
(For example, 2;IN;43). 

First value before semicolon should be digit 1 to 7. And last value after second semicolon should be 01 to 99 number.

My suggestion

@"/^\d[1-7];IN;\d{1,2}$/" 

does not work properly

Your should change your pattern.

1) / at the begin and end are not needed

2) \\d[1-7] is equivalent to [0-9][1-7] , but you need only one digit from 1 to 7

3) \\d{1,2} matches for one or two digits - in your case you need two digits (from 01 to 99 you should exclude 00 ).

Use this pattern for regular expression:

^[1-7];IN;(?!00)[0-9]{2}$
@"^[1-7];IN;[1-9]\d?$"

此版本可确保最后一部分大于0,并避免前导零。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM