简体   繁体   中英

match any a-z/A-Z and - character after certain regular expression

i need a certain string to be in this format: [0000] anyword

so between the [] brackets i need 4 numbers, followed by a whitespace. after that only characters ranging from a to z and - characters are allowed.

so this should be allowed:

[0000] foo-bar

[0000] foo

[0000] foo-bar-foo

etc..

so far i have this:

\[[0-9]{4}\]\s

this matches the [0000] , so it maches the brackets with 4 numbers in it and the whitespace. i can't seem to find something that allows charachters after that. i've tried putting a single "." at the end of the expression as this should match any character but this doesnt seem to be working.

\[[0-9]{4}\]\s^[A-Z]+[a-zA-Z]*$

the above isn't working either..

i need this expression as a Validationexpression for an asp.net custom validator.

any help will be appreciated

(\\[[0-9]{4}\\])\\s+([Az\\-]+) should hopefully work. It'll capture the numbers and letters into two capture groups as well.

试试这个

@"\[[0-9]{4}\] [a-zA-Z]+(-[a-zA-Z]+)*"

This works for your input: http://regexr.com/?30sb7 . Unlike Cornstalk's answer it does not capture anything, and - can indeed be placed later in a range if it's escaped.

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