[英]Regex for alphanumeric and the + character
我需要一個只允許使用字母數字加+和 - 字符的正則表達式。
現在我正在使用:
[^\w-]
以下模式將匹配僅包含字母,數字,“+”或“ - ”的字符串,包括國際字符,例如“å”或“ö”(並且不包括“ \\w
”中包含的“_”字符) :
^[-+\p{L}\p{N}]+$
例子:
string pattern = @"^[-+\p{L}\p{N}]+$";
Regex.IsMatch("abc", pattern); // returns true
Regex.IsMatch("abc123", pattern); // returns true
Regex.IsMatch("abc123+-", pattern); // returns true
Regex.IsMatch("abc123+-åäö", pattern); // returns true
Regex.IsMatch("abc123_", pattern); // returns false
Regex.IsMatch("abc123+-?", pattern); // returns false
Regex.IsMatch("abc123+-|", pattern); // returns false
只有在對具有字母數字字符和/或+
/ -
的字符串進行測試時,此正則表達式才會匹配:
^[a-zA-Z0-9\-+]+$
要使用它:
if (Regex.IsMatch(input, @"^[a-zA-Z0-9\-+]+$"))
{
// String only contains the characters you want.
}
嘗試這個 :
[a-zA-Z0-9+\-]
您必須轉義 - char: [\\w\\-+]
表示單個字符, [\\w\\-+]+
表示更多。
匹配單個,+或字母數字:
[-+a-zA-Z0-9]
匹配任意數量的 - ,+或字母數字:
[-+a-zA-Z0-9]*
匹配只是 - ,+或字母數字的字符串/行:
^[-+a-zA-Z0-9]*$
[A-ZA-Z0-9 \\ + \\ - ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.