package pack.java.thread.atm;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Expression {
//* + ? {n} {n,m}的意思:
//*代表0次到多次;
//+代表1次到多次;
//?代表0次到1次;
//{n}代表出现n次;
//{n,m}代表出现 n 到 m 次;
//\d代表0-9;
//\D代表非0-9;
//\s代表空格;
//\S代表非空格;
//\w代表A-Z0-9;
//\W代表非A-Z0-9;
/**
* 匹配字符串;
*/
private void testMethod1(){
Pattern pattern = Pattern.compile("Java.*",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("JAVA 编程语言!");
boolean bool = matcher.matches();
if(bool){
System.out.println("匹配成功!");
}else{
System.out.println("匹配失败!");
}
}
/***
* 分割字符串;
*/
private void testMethod2(){
String regx = "[,|]";
String input = "Hello Java Word Java,Hello ,,My,Name,Is|Java|Sun,.";
Pattern pattern = Pattern.compile(regx);
String[] strs = pattern.split(input);
for(int i = 0;i<strs.length;i++){
//如果不是空白的时候,则输出.
if(!"".equals(strs[i])){
System.out.println(strs[i]);
}
}
}
/**
* 替换;
*/
private void testMethod3(){
String regex = "正则表达式";
Pattern pattern = Pattern.compile(regex);
String input = "正则表达式 Hello Word! 正则表达式 Hello Word!";
Matcher matcher = pattern.matcher(input);
//替换第一个出现的位置;
String result = matcher.replaceFirst("Java");
//替换所有;
//String result = matcher.replaceAll("Java");
System.out.println(result);
}
/**
* 根据查找,然后进行替换;
*/
private void testMethod4(){
Pattern pattern = Pattern.compile("正则表达式");
Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "Java");
}
matcher.appendTail(sb);
System.out.println(sb.toString());
}
/**
* 验证邮箱;
*/
private void testMethod5(){
String regex = "[\\w\\.\\-]+@[\\w\\-]+[\\.]+[\\w]+";
Strinail;
//Pattern.CASE_INSENSITIVE 忽略大小写;
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
if(matcher.matches()){
System.out.println("是正确的邮箱!");
}else{
System.out.println("不是正确的邮箱!");
}
}
/**
* 去除html标记;
*/
private void testMethod6(){
Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher("<font size='5' color = 'red'>样式</font>");
//替换成空白;
String result = matcher.replaceAll("");
System.out.println(result);
}
/**
* 查找html中对应的字符;
*/
private void testMethod7(){
String regex = "href=\"(.+?)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("<a href=\"index.html\">主页</a>");
if(matcher.find()){
System.out.println(matcher.group(1));
}
}
/**
* 截取https或者http url;
*/
private void testMethod8(){
String regex = "(http://|https://){1}[\\w\\.\\-/:]+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("http://www.baidu.com -> http://www.csdn.net");
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
buffer.append(matcher.group());
buffer.append("\r\n");
}
System.out.println(buffer.toString());
}
/**
* 替换{0},{1},{2},{3}中的字符;
*
*/
private void testMethod9(){
String msg = "我喜欢的游戏有:{1},{2},{3},{4},{5}这几种.!";
String[] msgArray = new String[]{"泡泡堂","弹弹堂","Acrlive","卡卡跑订车","西游记"};
StringBuffer sb =new StringBuffer();
sb.append("我喜欢的游戏有:");
for(int i = 1;i<=msgArray.length;i++){
String regex ="\\{"+i+"\\}";
Pattern pattern =Pattern.compile(regex);
Matcher matcher= pattern.matcher(msg);
if(matcher.find()){
sb.append(msgArray[i-1]+",");
}
}
sb.append("这几种.!");
System.out.println(sb.toString());
String str = "Java目前的发展史是由{0}年-{1}年";
String[][] object={new String[]{"\\{0\\}","1995"},new String[]{"\\{1\\}","2007"}};
System.out.println(replace(str,object));
}
public static String replace(final String sourceString,Object[] object)
{
String temp=sourceString;
for(int i=0;i<object.length;i++){
String[] result=(String[])object[i];
Pattern pattern = Pattern.compile(result[0]);
Matcher matcher = pattern.matcher(temp);
temp=matcher.replaceAll(result[1]);
}
return temp;
};
public static void main(String[] args) {
Expression expression = new Expression();
expression.testMethod1();
expression.testMethod2();
expression.testMethod3();
expression.testMethod4();
expression.testMethod5();
expression.testMethod6();
expression.testMethod7();
expression.testMethod8();
expression.testMethod9();
}
}