简体   繁体   English

如何编写仅允许字母,数字和特殊字符(仅中间出现)的正则表达式

[英]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#: 这是我在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]+ 匹配必须以至少一个数字或字母字符开头: [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) 第二个组合可能发生零次或多次(例如: asdf-e*r!asdfa有效)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写仅匹配数字,文本,空格和仅两个特殊字符的正则表达式 - How to write regular expression that matches only numbers,text,spaces and only two special characters C#regular表达式,只允许使用字母和数字,但不允许任何特殊字符或空格 - C# regular Expression that allows only alphabets and numbers but not any special characters or spaces 正则表达式仅允许字母数字字符的属性 - Regular expression Attribute that only allows alpha numeric characters 正则表达式匹配仅包含数字而不包含字母的字符串 - Regular expression to match a string that contains only numbers, not letters 如何在C#中没有特殊字符或数字的情况下用字母(az)询问用户输入 - How to ask for user input with letters (a-z) only without special characters or numbers in C# 正则表达式,允许单词字符和一些特殊字符,但不能使用逗号 - Regular Expression that allows word characters and a few special characters but no commas 如何编写正则表达式以排除字符串中的某些特殊字符? - How to write a regular expression for excluding some special characters from string? 如何仅在C#中允许在正则表达式验证中使用有限的特殊字符? - How to allow limited special characters in regular expression validation only in c#? 带多个空格和特殊字符的数字的正则表达式? - regular expression for numbers with many spaces and special characters? 正则表达式只接受9位数字,没有特殊字符,可能只有前导零,最多2位 - Regular Expression accepting only 9 digits, no special characters , may have leading zero's only up to 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM