“正则表达式 验证复杂URL”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1Eq4y1E79W/?p=17”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1Eq4y1E79W/?p=17 | https://www.bilibili.com/video/BV1Eq4y1E79W/?p=17 | ||
+ | |||
+ | === 先确定 URL 的开始部分 === | ||
+ | |||
+ | ==== 示例:验证 https:// ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class RegExp11 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String content = "https://"; | ||
+ | |||
+ | /** | ||
+ | * 思路: | ||
+ | * 1、先确定 url 的开始部分 https:// | http:// | ||
+ | */ | ||
+ | String regStr = "^((http|https)://)$"; | ||
+ | |||
+ | Pattern pattern = Pattern.compile(regStr); | ||
+ | |||
+ | Matcher matcher = pattern.matcher(content); | ||
+ | |||
+ | if (matcher.find()) { | ||
+ | |||
+ | System.out.println("满足格式"); | ||
+ | |||
+ | } else { | ||
+ | |||
+ | System.out.println("不满足格式"); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | 满足格式 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | ==== 示例:验证 http:// ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class RegExp11 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String content = "http://"; | ||
+ | |||
+ | /** | ||
+ | * 思路: | ||
+ | * 1、先确定 url 的开始部分 https:// | http:// | ||
+ | */ | ||
+ | String regStr = "^((http|https)://)$"; | ||
+ | |||
+ | Pattern pattern = Pattern.compile(regStr); | ||
+ | |||
+ | Matcher matcher = pattern.matcher(content); | ||
+ | |||
+ | if (matcher.find()) { | ||
+ | |||
+ | System.out.println("满足格式"); | ||
+ | |||
+ | } else { | ||
+ | |||
+ | System.out.println("不满足格式"); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | 满足格式 | ||
+ | </syntaxhighlight> |
2022年11月19日 (六) 07:23的版本
https://www.bilibili.com/video/BV1Eq4y1E79W/?p=17
先确定 URL 的开始部分
示例:验证 https://
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp11 {
public static void main(String[] args) {
String content = "https://";
/**
* 思路:
* 1、先确定 url 的开始部分 https:// | http://
*/
String regStr = "^((http|https)://)$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
System.out.println("满足格式");
} else {
System.out.println("不满足格式");
}
}
}
满足格式
示例:验证 http://
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp11 {
public static void main(String[] args) {
String content = "http://";
/**
* 思路:
* 1、先确定 url 的开始部分 https:// | http://
*/
String regStr = "^((http|https)://)$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
System.out.println("满足格式");
} else {
System.out.println("不满足格式");
}
}
}
满足格式