Is there a difference between ^[a-zA-Z]
and [^a-zA-Z]
?
有什么区别^[a-zA-Z]和[^ a-zA-Z]吗?
When I check in C#,
当我检查c#时,
Regex.IsMatch("t", "^[a-zA-Z]") // Returns true (I think it's correct)
Regex.IsMatch("t", "[^a-zA-Z]") // Returns false
There are a lot of web sites using [^a-zA-Z]
for the alphabet. I'm not really sure which one is correct answer.
有很多网站使用[^ a-zA-Z]的字母表。我不确定哪一个是正确答案。
4 个解决方案
#1
55
Yes, the first means "match all strings that start with a letter", the second means "match all strings that contain a non-letter". The caret ("^") is used in two different ways, one to signal the start of the text, one to negate a character match inside square brackets.
是的,第一个意思是“匹配所有以字母开头的字符串”,第二个意思是“匹配所有包含非字母的字符串”。插入符号(“^”)是用于两种不同的方式,一个文本的开始信号,一个否定一个字符匹配方括号内。
#2
9
^[a-zA-Z]
means any a-z or A-Z at the start of a line
^[a-zA-Z]意味着任何a - z、a - z在一行的开始
[^a-zA-Z]
means any character that IS NOT a-z OR A-Z
[^ a-zA-Z]意味着任何字符不是a - z、a - z
#3
5
There is a difference.
有一个区别。
When the ^
character appears outside of []
matches the beginning of the line (or string). When the ^
character appears inside the []
, it matches any character not appearing inside the []
.
当“字符”出现在[]之外时,就匹配行(或字符串)的开头。当^字符出现在[],它匹配任何字符不出现在[]。
#4
3
^ outside of the character class ("[a-zA-Z]") notes that it is the "begins with" operator.
^ inside of the character negates the specified class.
^以外的字符类(“[a-zA-Z]”)指出,这是“始于”操作符。^字符否定内部指定的类。
So, "^[a-zA-Z]" translates to "begins with character from a-z or A-Z", and "[^a-zA-Z]" translates to "is not either a-z or A-Z"
所以,“^[a-zA-Z]”转化为“从a - z、a - z开头字符”,和“[^ a-zA-Z]”转化为“不是a - z、a - z”
Here's a quick reference: http://www.regular-expressions.info/reference.html
这里有一个快速的参考:http://www.regular-expressions.info/reference.html。
#1
55
Yes, the first means "match all strings that start with a letter", the second means "match all strings that contain a non-letter". The caret ("^") is used in two different ways, one to signal the start of the text, one to negate a character match inside square brackets.
是的,第一个意思是“匹配所有以字母开头的字符串”,第二个意思是“匹配所有包含非字母的字符串”。插入符号(“^”)是用于两种不同的方式,一个文本的开始信号,一个否定一个字符匹配方括号内。
#2
9
^[a-zA-Z]
means any a-z or A-Z at the start of a line
^[a-zA-Z]意味着任何a - z、a - z在一行的开始
[^a-zA-Z]
means any character that IS NOT a-z OR A-Z
[^ a-zA-Z]意味着任何字符不是a - z、a - z
#3
5
There is a difference.
有一个区别。
When the ^
character appears outside of []
matches the beginning of the line (or string). When the ^
character appears inside the []
, it matches any character not appearing inside the []
.
当“字符”出现在[]之外时,就匹配行(或字符串)的开头。当^字符出现在[],它匹配任何字符不出现在[]。
#4
3
^ outside of the character class ("[a-zA-Z]") notes that it is the "begins with" operator.
^ inside of the character negates the specified class.
^以外的字符类(“[a-zA-Z]”)指出,这是“始于”操作符。^字符否定内部指定的类。
So, "^[a-zA-Z]" translates to "begins with character from a-z or A-Z", and "[^a-zA-Z]" translates to "is not either a-z or A-Z"
所以,“^[a-zA-Z]”转化为“从a - z、a - z开头字符”,和“[^ a-zA-Z]”转化为“不是a - z、a - z”
Here's a quick reference: http://www.regular-expressions.info/reference.html
这里有一个快速的参考:http://www.regular-expressions.info/reference.html。