吴裕雄--天生自然 JAVA开发学习:正则表达式
2024-08-29 02:44:36
import java.util.regex.*; class RegexExample1{
public static void main(String args[]){
String content = "I am noob " +
"from runoob.com."; String pattern = ".*runoob.*"; boolean isMatch = Pattern.matches(pattern, content);
System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
public static void main( String args[] ){ // 按指定模式在字符串查找
String line = "This order was placed for QT3000! OK?";
String pattern = "(\\D*)(\\d+)(.*)"; // 创建 Pattern 对象
Pattern r = Pattern.compile(pattern); // 现在创建 matcher 对象
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
System.out.println("Found value: " + m.group(3) );
} else {
System.out.println("NO MATCH");
}
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
private static final String REGEX = "\\bcat\\b";
private static final String INPUT =
"cat cat cat cattie cat"; public static void main( String args[] ){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 获取 matcher 对象
int count = 0; while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
}
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooooooooo";
private static final String INPUT2 = "ooooofoooooooooooo";
private static Pattern pattern;
private static Matcher matcher;
private static Matcher matcher2; public static void main( String args[] ){
pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT);
matcher2 = pattern.matcher(INPUT2); System.out.println("Current REGEX is: "+REGEX);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("Current INPUT2 is: "+INPUT2); System.out.println("lookingAt(): "+matcher.lookingAt());
System.out.println("matches(): "+matcher.matches());
System.out.println("lookingAt(): "+matcher2.lookingAt());
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. " +
"All dogs say meow.";
private static String REPLACE = "cat"; public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoobkkk";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// 获取 matcher 对象
Matcher m = p.matcher(INPUT);
StringBuffer sb = new StringBuffer();
while(m.find()){
m.appendReplacement(sb,REPLACE);
}
m.appendTail(sb);
System.out.println(sb.toString());
}
}
最新文章
- 【记录】Install-Package : “Unity”已拥有为“CommonServiceLocator”定义的依赖项。
- gitlab多人协作开发
- CentOS下安装Tomcat7
- git技巧记录--blame
- 日期String相互转换
- C# string类型和byte[]类型相互转换
- http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html
- css系列-间隔与间距实例
- ORM之四:调用入口与调用示例
- nodejs安装不了和npm安装不了的解决方法
- Delphi中使用GDI+进行绘图(2)
- asp.net mvc上传图片案例
- 第三章 Python 的容器: 列表、元组、字典与集合
- 《JAVASCRIPT高级程序设计》根植于原型链的继承
- PHP学习笔记-4(时间戳)
- C#之面向对象的特性
- python学习第九讲,python中的数据类型,字符串的使用与介绍
- Spring Data JPA方法定义规范
- Linux文件系统命令 ln
- shiro实战系列(八)之安全管理器
热门文章
- 九十七、SAP中ALV事件之十,通过REUSE_ALV_COMMENTARY_WRITE函数来显示ALV的标题
- 十八、SAP中使用IF/ELSE判断语句,以及sy-subrc的用法
- 笔记本如何不按Fn键就能实现F键的功能
- junit基础学习之-测试service层(3)
- hdu 3308 线段树,单点更新 求最长连续上升序列长度
- Sublime 一些常用快捷键
- webpack散记---提取公共代码
- C++的随机数
- Day 6:Vector类和实现Hashset以及登录窗口的模拟
- 「黑科技」智能消毒防疫机器人 技术方案介绍-disinfection robot