简体   繁体   中英

Regex is not validation string

Hi I have below regular expression which I am using to validate string expression:

Regex: ^\d{9}_[a-zA-Z]{1}_(0[1-9]|1[0-2]).(0[1-9]|[1-2][0-9]|3[0-1]).[0-9]{4}_[0-9]{3}_[0-9a-zA-Z]{2}(?:_[0-9a-zA-Z]*)?

String :999999999_A_12.31.2015_000_3Q_cdvknv

What i was expecting is if user enters below string it should not match with my regular expression:

String :999999999_A_12.31.2015_000_3Qwe_cdvknv

But instead of showing as wrong form it was checking for match case i tried using online validator but couldn't find correct one. Please suggest. I am using vb.net.

I think you can solve the issue by adding an anchor:

^\d{9}_[a-zA-Z]_(0[1-9]|1[0-2]).(0[1-9]|[1-2][0-9]|3[0-1]).[0-9]{4}_[0-9]{3}_[0-9a-zA-Z]{2}(?:_[0-9a-zA-Z]*)?$
                                                                                                             ^

See demo

The $ is an end-of-string anchor and will force the string to end after 3Q . If there are any more characters but not _xxx , the match will fail.

Note that if you need to check for a partial match inside a larger sentence, you might as well use \\b word boundary instead of $ .

And note that {1} is redundant.

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