简体   繁体   中英

How to Write a Regular Expression that allows only letters, numbers and special characters only i the middle never in the end

Need some regular expressions help.

So far I have my code working to allow a limited number of special characters. However, I don't know how to get it to only allow them in the middle and never the end.

Can someone help me figure that part out?

Here is the code I am using in c#:

            Regex uRLToVal= new Regex("^[A-Za-z0-9-_.+!*]*$");

            if (!uRLToVal.IsMatch(this.mainURL))
            {
                results.AddPropertyError("Your Entry can only contain letters, numbers, underscores, periods, plus, exclamation marks and hypens. Special characters should always be inside numbers or letters.  Example: v!v is OK BUT NOT !vv or vv!");
            }
new Regex(@"^[A-Za-z0-9]+([-_.+!*]+[A-Za-z0-9]+)*$");

which means :

match must begin with at least one numeric or alpha char: [AZ-z0-9]+

That sequence may be followed by zero or many special chars -_.+!* followed by at least one numeric or alpha char. This second combination can happen zero or many times (example : asdf-e*r!asdf , or a are valid)

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