“正则表达式 Pattern 类”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) (→示例2:) |
||
第35行: | 第35行: | ||
整体匹配=false | 整体匹配=false | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
==== 示例2: ==== | ==== 示例2: ==== |
2022年11月19日 (六) 10:47的版本
https://www.bilibili.com/video/BV1Eq4y1E79W/?p=18
Pattern 对象是一个正则表达式对象。
Pattern 没有公共构造方法。
要创建一个 Pattern 对象,调用其公共静态方法,它返回一个 Pattern 对象。
该方法接受一个正则表达式作为它的第一个参数,比如:
Pattern pattern = Pattern.compile(regStr);
示例:演示 matches 方法进行整体匹配判断
示例1:
import java.util.regex.Pattern;
public class PatternMethod {
public static void main(String[] args) {
String content = "hello abc hello,韩顺平教育";
String regStr = "hello";
boolean matches = Pattern.matches(regStr, content);
System.out.println("整体匹配=" + matches);
}
}
整体匹配=false
示例2:
import java.util.regex.Pattern;
public class PatternMethod {
public static void main(String[] args) {
String content = "hello abc hello,韩顺平教育";
String regStr = "hello.*";
boolean matches = Pattern.matches(regStr, content);
System.out.println("整体匹配=" + matches);
}
}
整体匹配=true
import java.util.regex.Pattern;
public class Hello {
public static void main(String[] args) {
String content = "I am hsp from hspedu.com.";
String pattern = ".*hspedu.*";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println("是否整体匹配成功:" + isMatch);
}
}
是否整体匹配成功:true
断点调试,跟踪源代码