Java6学习笔记40——JAVA中的正则表达

1.简介:

正则表达式是Java 处理字符串、文本的重要工具。

java.util.regex.Pattern 匹配类:用模式匹配一个字符串所表达的抽象结果。

java.util.regex.Matcher 模式类:用来表示一个编译过的正则表达式。


2.例子

import java.util.regex.Pattern;

public class RegexDemo1 {
public static void main(String arg[]) {
String expr;
String str;

expr = "p.t";
str = "pot";
boolean b = Pattern.matches(expr,str);//静态方法,直接调用
if(b)
System.out.println(str + " matches pattern " + expr);
else
System.out.println(str + " does not match pattern " + expr);
}
}

但是这个例子中的expr每次都要进行编译才能进行匹配,而下面的例子则不需要。

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexDemo2 {
public static void main(String arg[]) {
String expr;
String str;

expr = "p\\wt";//在Java语言中,\\表示要插入正则表达式的反斜线,且后面的字符有特殊意义。\w 单词字符
str = "pot";
Pattern pattern = Pattern.compile(expr);
Matcher matcher = pattern.matcher(str);
if(matcher.matches())
System.out.println(str + " matches pattern " + expr);
else
System.out.println(str + " does not match pattern " + expr);
}
}

以上这个例子中的pattern就可以重用了,比如:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexDemo3 {
public static void main(String arg[]) {
String expr;

expr = "p[aeiou]t";
Pattern pattern = Pattern.compile(expr);
for(int i=0; i<arg.length; i++) {
Matcher matcher = pattern.matcher(arg[i]);
if(matcher.matches())
System.out.println(arg[i] + " matches pattern " + expr);
else
System.out.println(arg[i] + " does not match pattern " + expr);
}
}
}

查找在一个段落中的匹配地:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexDemo4 {
public static void main(String arg[]) {
String expr;
String str;

expr = "bark";
str = "This is a larger block of text the pattern matcher " +
"will search for the word bark. The pattern is very " +
"simple. It will only match the word bark, and nothing " +
"else. You can search a block of text for any of the " +
"regular expression patterns--not just bark.";
Pattern pattern = Pattern.compile(expr);
Matcher matcher = pattern.matcher(str);
boolean found = matcher.find();
while(found) {
int location = matcher.start();
System.out.println(expr + " found at " + location);
found = matcher.find(location+1);
}
}
}
=================

综合例子:

importjava.util.regex.Matcher;
importjava.util.regex.Pattern;

/**
* 正则表达式例子
*
* @author leizhimin 2009-7-17 9:02:53
*/
publicclassTestRegx {
publicstaticvoidmain(String[] args) {
Pattern p = Pattern.compile("f(.+?)k");
Matcher m = p.matcher("fckfkkfkf");
while(m.find()) {
String s0 = m.group();
String s1 = m.group(1);
System.out.println(s0 +"||"+ s1);
}
System.out.println("---------");
m.reset("fucking!");
while(m.find()) {
System.out.println(m.group());
}

Pattern p1 = Pattern.compile("f(.+?)i(.+?)h");
Matcher m1 = p1.matcher("finishabigfishfrish");
while(m1.find()) {
String s0 = m1.group();
String s1 = m1.group(1);
String s2 = m1.group(2);
System.out.println(s0 +"||"+ s1 +"||"+ s2);
}

System.out.println("---------");
Pattern p3 = Pattern.compile("(19|20)\\d\\d([- /.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])");
Matcher m3 = p3.matcher("1900-01-01 2007/08/13 1900.01.01 1900 01 01 1900-01.01 1900 13 01 1900 02 31");
while(m3.find()) {
System.out.println(m3.group());
}
}
}
输出结果:
fck||c
fkk||k
---------
fuck
finish||in||s
fishfrish||ishfr||s
---------
1900-01-01
2007/08/13
1900.01.01
1900 01 01
1900 02 31

Process finished with exit code 0

你可能感兴趣的:(java,正则表达式,F#,D语言)