我有一个域和子域的列表,我正在检查列表中的每个项目是否都是有效的域名(或子域),例如: www.google.com - google.com - drive.google.com 。
这是我的正则表达式:^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$
这是我的代码:
// Validate Domains
private boolean validateDomains (String domains) {
String domainsList[] = domains.split("\\n");
final Pattern domainPattern = Pattern.compile("^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$");
for (int i = 0; i < domainsList.length; i++) {
if (!domainPattern.matcher(domainsList[i]).matches()) {
return false;
}
}
return true;
}
此代码永远不会通过测试!
答案 0 :(得分:3)
或者,也许您将为表达式添加更多边界,类似于:
(?i)^(?:https?:\/\/)?(?:www\.)?(?:[a-z0-9-]+\.){1,9}[a-z]{2,5}(?:\/.*)?$
或
(?i)^(?:https?:\/\/)?(?:www\.)?(?:[a-z0-9-]{1,20}\.){1,9}[a-z]{2,5}(?:\/.*)?$
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class re{
public static void main(String[] args){
final String regex = "(?i)^(?:https?:\\/\\/)?(?:www\\.)?(?:[a-z0-9-]+\\.){1,9}[a-z]{2,5}(?:\\/.*)?$";
final String string = "www.google.com\n"
+ "google.com\n"
+ "drive.google.com\n"
+ "http://www.google.com\n"
+ "http://google.com\n"
+ "http://drive.google.com\n"
+ "https://www.google.com\n"
+ "https://www.google.com\n"
+ "https://www.drive.google.com\n"
+ "https://www.google.com/some_other_things\n"
+ "https://www.google.com/\n"
+ "https://www.drive.google.com/\n"
+ "https://www.a.a.a.a.a.a.a.a.a.google.com/";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Full match: www.google.com
Full match: google.com
Full match: drive.google.com
Full match: http://www.google.com
Full match: http://google.com
Full match: http://drive.google.com
Full match: https://www.google.com
Full match: https://www.google.com
Full match: https://www.drive.google.com
Full match: https://www.google.com/some_other_things
Full match: https://www.google.com/
Full match: https://www.drive.google.com/
如果您希望简化/修改/探索表达式,请在regex101.com的右上角进行说明。如果愿意,您还可以在this link中查看它如何与某些示例输入匹配。
jex.im可视化正则表达式:
答案 1 :(得分:2)
我为该代码找到了正确的正则表达式!
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
该RegEx对我来说非常有效。
现在这是我的代码:
// Validate Domains
private boolean validateDomains (String domains) {
String domainsList[] = domains.split("\\n");
final Pattern domainPattern = Pattern.compile("^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$");
for (int i = 0; i < domainsList.length; i++) {
if (!domainPattern.matcher(domainsList[i]).matches()) {
return false;
}
}
return true;
}
答案 2 :(得分:0)
尝试一下
private boolean validateDomains (String domains) {
return Arrays.stream(domains.split("\\n")).allMatch(domain -> domain.matches("\"^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\\\.)+[A-Za-z]{2,6}$\""));
}