📄 regtest.java
字号:
package com.gctech.sms.sdsms.common;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <p>Title:正则表达式试验类</p>
* <p>Description:</p>
* <p>Copyright: GCTech (c) 2004-6-24</p>
* <p>Company: 国创科技</p>
* <p>Email: ly@gctech.com.cn</p>
*
*
* @version 1.0
* @author liyi
*/
public class RegTest {
public static void find() {
Pattern p = null; //正则表达式
Matcher m = null; //操作的字符串
boolean b;
String s = null;
StringBuffer sb = null;
int i = 0;
//字符串查找
p = Pattern.compile("cat");
m = p.matcher("one cat two cats in the yard");
sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
i++;
}
m.appendTail(sb);
System.out.println(sb.toString() + "<br>");
System.out.println(i + "<br>");
i = 0;
p = Pattern.compile("cat");
m = p.matcher("one cat two ca tsi nthe yard");
sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
i++;
}
m.appendTail(sb);
System.out.println(sb.toString() + "<br>");
System.out.println(i + "<br>");
p = Pattern.compile("cat");
m = p.matcher("one cat two cats in the yard");
p = m.pattern();
m = p.matcher("bacatab");
b = m.matches();
System.out.println(b + "<br>");
s = m.replaceAll("dog");
System.out.println(s + "<br>");
i = 0;
p = Pattern.compile("(fds){2,}");
m = p.matcher("dsa da fdsfds aaafdsafds aaf");
sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
i++;
}
m.appendTail(sb);
System.out.println(sb.toString() + "<br>");
System.out.println(i + "<br>");
p = Pattern.compile("cat");
m = p.matcher("one cat two cats in the yard");
sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "<font color=\"red\">cat</font>");
}
m.appendTail(sb);
System.out.println(sb.toString() + "<br>");
String aa = sb.toString();
System.out.println(aa + "<br>");
}
public static void main(String[] args) {
find();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -