正则表达式末尾不允许有特殊字符,包括下划线,并且只有 3 个字符限制
Regular expression for not allowing special chars at the end including underscore and with only 3 character limit
- 我的要求是"Name should begin with an alphabet and should have atleast three chars and cannot end with a special character,But the special chars can come in the middle ."最后也不应该允许下划线。
- 我正在使用的正则表达式
[a-zA-Z][\w]{1,}.*[\w]
- 以上正则表达式无法将下划线
(_)
识别为末尾的特殊字符。
- 当我键入 "sss_" 时无法识别。
当我键入 "sss@" 或 "sss#" 或 "sss$" 时,它会识别。
预期结果"test","test@test","test_test","tes"
意外结果"tes@"、"test_"、"te"
更新 2:
既然您现在说它在 Android 中,请删除 (?U)
。在 Android 正则表达式中,所有 shorthand 字符 类 都已经支持 Unicode:
"\p{L}+(?:[\W_]\p{L}+)*"
并与 matches()
一起使用。
更新答案
使用
Boolean isValid = groupNameString.matches("(?U)\p{L}+(?:[\W_]\p{L}+)*");
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(validateName("Dąb_rośnie$gdzieś#tu"));
System.out.println(validateName("Some_string_here"));
}
private static boolean validateName(String name) {
if (name.matches("(?U)\p{L}+(?:[\W_]\p{L}+)*")) {
return true;
}
return false;
}
}
请注意,使用 String#matches()
时甚至不需要锚点,因为此方法默认锚定模式。
[\W_]
将匹配任何特殊字符 inlcuding a _
.
原回答
要匹配 letters
+ ( _
+ letters
) *
模式,您可以使用
^[^\W\d_]+(?:_[^\W\d_]+)*$
您可以在 JS 或任何其他不支持 Unicode 的引擎中使用 [a-zA-Z]
而不是 [^\W\d_]
,并且您只需要处理 ASCII 字母。
支持 Unicode 的等价物:
^\p{L}+(?:_\p{L}+)*$
此处,\p{L}
匹配任何 Unicode 字母。
详情:
^
- 字符串锚点的开始
[^\W\d_]+
/ \p{L}+
- 1 个或多个字母
(?:_[^\W\d_]+)*
- 零次或多次出现:
_
- 一个_
[^\W\d_]+
/ \p{L}+
- 1 个或多个字母
$
- 字符串锚结束。
参见 this regex demo。
试试这个正则表达式
^[A-z]+[^\`|\~|\!|\@|\#|$|\%|\^|\&|\_|\*|\(|\)|\+|\=|\[|\{|\]|\}|\||\|\'|\<|\,|\.|\>|\?|\/|\""|\;|\:|\s]+$
- 我的要求是"Name should begin with an alphabet and should have atleast three chars and cannot end with a special character,But the special chars can come in the middle ."最后也不应该允许下划线。
- 我正在使用的正则表达式
[a-zA-Z][\w]{1,}.*[\w]
- 以上正则表达式无法将下划线
(_)
识别为末尾的特殊字符。 - 当我键入 "sss_" 时无法识别。
当我键入 "sss@" 或 "sss#" 或 "sss$" 时,它会识别。
预期结果"test","test@test","test_test","tes"
意外结果"tes@"、"test_"、"te"
更新 2:
既然您现在说它在 Android 中,请删除 (?U)
。在 Android 正则表达式中,所有 shorthand 字符 类 都已经支持 Unicode:
"\p{L}+(?:[\W_]\p{L}+)*"
并与 matches()
一起使用。
更新答案
使用
Boolean isValid = groupNameString.matches("(?U)\p{L}+(?:[\W_]\p{L}+)*");
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(validateName("Dąb_rośnie$gdzieś#tu"));
System.out.println(validateName("Some_string_here"));
}
private static boolean validateName(String name) {
if (name.matches("(?U)\p{L}+(?:[\W_]\p{L}+)*")) {
return true;
}
return false;
}
}
请注意,使用 String#matches()
时甚至不需要锚点,因为此方法默认锚定模式。
[\W_]
将匹配任何特殊字符 inlcuding a _
.
原回答
要匹配 letters
+ ( _
+ letters
) *
模式,您可以使用
^[^\W\d_]+(?:_[^\W\d_]+)*$
您可以在 JS 或任何其他不支持 Unicode 的引擎中使用 [a-zA-Z]
而不是 [^\W\d_]
,并且您只需要处理 ASCII 字母。
支持 Unicode 的等价物:
^\p{L}+(?:_\p{L}+)*$
此处,\p{L}
匹配任何 Unicode 字母。
详情:
^
- 字符串锚点的开始[^\W\d_]+
/\p{L}+
- 1 个或多个字母(?:_[^\W\d_]+)*
- 零次或多次出现:_
- 一个_
[^\W\d_]+
/\p{L}+
- 1 个或多个字母
$
- 字符串锚结束。
参见 this regex demo。
试试这个正则表达式
^[A-z]+[^\`|\~|\!|\@|\#|$|\%|\^|\&|\_|\*|\(|\)|\+|\=|\[|\{|\]|\}|\||\|\'|\<|\,|\.|\>|\?|\/|\""|\;|\:|\s]+$