87,990
社区成员
发帖
与我相关
我的任务
分享
//上面的java代码,其实是由三个部分合并而成,请看下面的做法
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegex {
public static void main(String[] args) {
String[] sourceStrings = {
"-test","_test","test_","test_123","test-","test-123","123"//这是你给出的错误例子
,"test","test_test123","test-test123"//这是你给出的正确例子
,"1test2","te1st","1test2","test2"//不存在-_时,至少有一个字母
,"1te-st2A","te-1stA","1te-st2A","te-st2A"//存在-_时(不能在两头),且结尾是字母时
,"1te-st2","te-1st2","1te-st2","te-st2"//存在-_时(不能在两头),且结尾是数字时
};//不存在-_时,至少有一个字母
String regex = "^[a-zA-Z\\d]+[a-zA-Z][a-zA-Z\\d]*$";//不存在-_时,至少有一个字母
regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[a-zA-Z]$";//存在-_时(不能在两头),且结尾是字母时
regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$";//存在-_时(不能在两头),且结尾是数字时
//以字母、数字和'-'和'_'组成,
//命名必需以非'-'和'_'开头,
//末尾不能是'_'或者'_'加数字,末尾不能是‘-’或者‘-’加数字,
//不能以纯数字命名。
Pattern pattern = Pattern.compile(regex);
for(int i=0; i<sourceStrings.length; i++){
System.out.println("----------------------------------");
System.out.println(sourceStrings[i] + " 匹配吗? " );
System.out.println(pattern.matcher(sourceStrings[i]).find());
}
}
}
/*
----------------------------------
-test 匹配吗?
false
----------------------------------
_test 匹配吗?
false
----------------------------------
test_ 匹配吗?
false
----------------------------------
test_123 匹配吗?
false
----------------------------------
test- 匹配吗?
false
----------------------------------
test-123 匹配吗?
false
----------------------------------
123 匹配吗?
false
----------------------------------
test 匹配吗?
true
----------------------------------
test_test123 匹配吗?
true
----------------------------------
test-test123 匹配吗?
true
----------------------------------
1test2 匹配吗?
true
----------------------------------
te1st 匹配吗?
true
----------------------------------
1test2 匹配吗?
true
----------------------------------
test2 匹配吗?
true
----------------------------------
1te-st2A 匹配吗?
true
----------------------------------
te-1stA 匹配吗?
true
----------------------------------
1te-st2A 匹配吗?
true
----------------------------------
te-st2A 匹配吗?
true
----------------------------------
1te-st2 匹配吗?
true
----------------------------------
te-1st2 匹配吗?
true
----------------------------------
1te-st2 匹配吗?
true
----------------------------------
te-st2 匹配吗?
true
*/
import java.util.regex.Pattern;
public class TestRegex {
public static void main(String[] args) {
String[] sourceStrings = {
"-test","_test","test_","test_123","test-","test-123","123"//这是你给出的错误例子
,"test","test_test123","test-test123"//这是你给出的正确例子
,"1test2","te1st","1test2","test2"//不存在-_时,至少有一个字母
,"1te-st2A","te-1stA","1te-st2A","te-st2A"//存在-_时(不能在两头),且结尾是字母时
,"1te-st2","te-1st2","1te-st2","te-st2"//存在-_时(不能在两头),且结尾是数字时
};//不存在-_时,至少有一个字母
String regex = "^[a-zA-Z\\d]+([a-zA-Z][a-zA-Z\\d]|[a-zA-Z\\d-_]*([a-zA-Z]|[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]))$";
//以字母、数字和'-'和'_'组成,
//命名必需以非'-'和'_'开头,
//末尾不能是'_'或者'_'加数字,末尾不能是‘-’或者‘-’加数字,
//不能以纯数字命名。
Pattern pattern = Pattern.compile(regex);
for(int i=0; i<sourceStrings.length; i++){
System.out.println("----------------------------------");
System.out.println(sourceStrings[i] + " 匹配吗? " );
System.out.println(pattern.matcher(sourceStrings[i]).find());
}
}
}
/*output:
----------------------------------
-test 匹配吗?
false
----------------------------------
_test 匹配吗?
false
----------------------------------
test_ 匹配吗?
false
----------------------------------
test_123 匹配吗?
false
----------------------------------
test- 匹配吗?
false
----------------------------------
test-123 匹配吗?
false
----------------------------------
123 匹配吗?
false
----------------------------------
test 匹配吗?
true
----------------------------------
test_test123 匹配吗?
true
----------------------------------
test-test123 匹配吗?
true
----------------------------------
1test2 匹配吗?
true
----------------------------------
te1st 匹配吗?
true
----------------------------------
1test2 匹配吗?
true
----------------------------------
test2 匹配吗?
true
----------------------------------
1te-st2A 匹配吗?
true
----------------------------------
te-1stA 匹配吗?
true
----------------------------------
1te-st2A 匹配吗?
true
----------------------------------
te-st2A 匹配吗?
true
----------------------------------
1te-st2 匹配吗?
true
----------------------------------
te-1st2 匹配吗?
true
----------------------------------
1te-st2 匹配吗?
true
----------------------------------
te-st2 匹配吗?
true
*/
//不好意思合并的时候 出了点误差,把*误看做是+了
//原始是这样的
regex = "^[a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*$";//不存在-_时,至少有一个字母
regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[a-zA-Z]$";//存在-_时(不能在两头),且结尾是字母时
regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$";//存在-_时(不能在两头),且结尾是数字时
//合并后(现在的版本):
regex = "^[a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*$|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*([a-zA-Z]$|[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$)";
<SCRIPT Language="VBScript">
Function reg(Str)
Dim Re,Match,Matches
Set Re = New Regexp
Re.Global = True
Re.Ignorecase = True
Re.Pattern="^\b[a-zA-Z0-9][a-zA-Z0-9_\-]+[a-zA-Z0-9]\b$"
ck = re.Replace(Str,"")
If ck = "" Then
reg = "合法"
Else
reg = "不合法"
Exit Function
End If
Re.Pattern="^\b[0-9]+\b$"
ck = re.Replace(Str,"")
If ck = "" Then
reg = "不合法"
Exit Function
Else
reg = "合法"
End If
Re.Pattern="^\b.*[\_|\-][0-9]*\b$"
ck = re.Replace(Str,"")
If ck = "" Then
reg = "不合法"
Exit Function
Else
reg = "合法"
End If
End Function
arr = "-test,_test,test_,test_123,test-,test-123,123,test,test_test123,test-test123,1test2,te1st,1test2,test2,1te-st2A,te-1stA,1te-st2A,te-st2A,1te-st2,te-1st2,1te-st2,te-st2"
arr = Split(arr,",")
For i = 0 To UBound(arr)
Document.write arr(i) & "-" & reg(arr(i)) & "<br>"
next
</SCRIPT>
//以字母、数字和'-'和'_'组成,