是否有正则表达式检查字符串是否只包含大写字母、小写字母、数字和下划线?
当前回答
试试我为字符串做的这些多语言扩展。
isalphannumeric—字符串必须包含至少一个alpha(在Unicode范围内的字母,在charSet中指定)和至少一个数字(在numSet中指定)。同样,字符串应该只包含alpha和数字。
IsAlpha -字符串应该包含至少一个alpha(在指定的语言字符集中),并且只由alpha组成。
IsNumeric -字符串应该至少包含一个数字(使用指定的语言numSet),并且仅由数字组成。
可以指定所需语言的charSet/numSet范围。Unicode范围可在Unicode图表。
API:
public static bool IsAlphaNumeric(this string stringToTest)
{
// English
const string charSet = "a-zA-Z";
const string numSet = @"0-9";
// Greek
//const string charSet = @"\u0388-\u03EF";
//const string numSet = @"0-9";
// Bengali
//const string charSet = @"\u0985-\u09E3";
//const string numSet = @"\u09E6-\u09EF";
// Hindi
//const string charSet = @"\u0905-\u0963";
//const string numSet = @"\u0966-\u096F";
return Regex.Match(stringToTest, @"^(?=[" + numSet + @"]*?[" + charSet + @"]+)(?=[" + charSet + @"]*?[" + numSet + @"]+)[" + charSet + numSet +@"]+$").Success;
}
public static bool IsNumeric(this string stringToTest)
{
//English
const string numSet = @"0-9";
//Hindi
//const string numSet = @"\u0966-\u096F";
return Regex.Match(stringToTest, @"^[" + numSet + @"]+$").Success;
}
public static bool IsAlpha(this string stringToTest)
{
//English
const string charSet = "a-zA-Z";
return Regex.Match(stringToTest, @"^[" + charSet + @"]+$").Success;
}
用法:
// English
string test = "AASD121asf";
// Greek
//string test = "Ϡϛβ123";
// Bengali
//string test = "শর৩৮";
// Hindi
//string test = @"क़लम३७ख़";
bool isAlphaNum = test.IsAlphaNumeric();
其他回答
要求的格式
允许以下三点:
0142171547295 014 - 2171547295 123年美国广播公司
不允许其他格式:
validatePnrAndTicketNumber(){
let alphaNumericRegex=/^[a-zA-Z0-9]*$/;
let numericRegex=/^[0-9]*$/;
let numericdashRegex=/^(([1-9]{3})\-?([0-9]{10}))$/;
this.currBookingRefValue = this.requestForm.controls["bookingReference"].value;
if(this.currBookingRefValue.length == 14 && this.currBookingRefValue.match(numericdashRegex)){
this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
}else if(this.currBookingRefValue.length ==6 && this.currBookingRefValue.match(alphaNumericRegex)){
this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
}else if(this.currBookingRefValue.length ==13 && this.currBookingRefValue.match(numericRegex) ){
this.requestForm.controls["bookingReference"].setErrors({'pattern': false});
}else{
this.requestForm.controls["bookingReference"].setErrors({'pattern': true});
}
}
<input name="booking_reference" type="text" [class.input-not-empty]="bookingRef.value"
class="glyph-input form-control floating-label-input" id="bookings_bookingReference"
value="" maxlength="14" aria-required="true" role="textbox" #bookingRef
formControlName="bookingReference" (focus)="resetMessageField()" (blur)="validatePnrAndTicketNumber()"/>
你想要检查每个字符是否符合你的要求,这就是为什么我们使用:
[A-Za-z0-9_]
你甚至可以用简写的形式:
\w
这是等价的(在某些正则表达式中,所以在使用它之前一定要检查一下)。然后,为了表明整个字符串必须匹配,您使用:
^
若要指示字符串必须以该字符开头,则使用
$
若要指示字符串必须以该字符结束。然后使用
\w+ or \w*
表示“1或多个”,或“0或多个”。综上所述,我们有:
^\w*$
这里有很多冗长的内容,我非常反对,所以,我的结论是:
/^\w+$/
\w等价于[A-Za-z0-9_],这就是你想要的(除非我们在混合中引入Unicode)。
使用+量词可以匹配一个或多个字符。如果你也想接受一个空字符串,使用*代替。
下面的正则表达式匹配字母数字字符和下划线:
^[a-zA-Z0-9_]+$
例如,在Perl中:
#!/usr/bin/perl -w
my $arg1 = $ARGV[0];
# Check that the string contains *only* one or more alphanumeric chars or underscores
if ($arg1 !~ /^[a-zA-Z0-9_]+$/) {
print "Failed.\n";
} else {
print "Success.\n";
}
下面是一个正则表达式,用于使用量词指定至少1个字符且不超过255个字符
[^a-zA-Z0-9 _]{1,255}
推荐文章
- 如何从JavaScript中使用正则表达式的字符串中剥离所有标点符号?
- 正则表达式中的单词边界是什么?
- 如何将一个标题转换为jQuery的URL段塞?
- Javascript和regex:分割字符串并保留分隔符
- (grep)正则表达式匹配非ascii字符?
- 如何在保持原始字符串的同时对字符串执行Perl替换?
- 创建正则表达式匹配数组
- *的区别是什么?和。*正则表达式?
- 如何将“camelCase”转换为“Camel Case”?
- 在Java中使用正则表达式提取值
- Java中的正则表达式命名组
- 使用正则表达式搜索和替换Visual Studio代码
- 使用split("|")按管道符号拆分Java字符串
- 替换字符串中第一次出现的模式
- “\d”在正则表达式中是数字吗?