写了正则的介绍好长时间,不动了。看到有人写了写关于正则的java应用,毕竟我们这里是javaResearch :)。所以写了些代码,为了简单所以什么也没有使用,只要考到本地就可以看到输出。
下面是代码:大家应该都看得懂,全当是自己记录。
package simpleTest;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.regexp.RE;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Edgar(wxt)
*
*/
public class TestForRex {
static String rexPatternString = "^((http|https)://[^/]*/).*";
static String rexSearchString = "http://10.4.8.43/makdsm/amd.do";
/**
* @param args
*/
public static void main(String[] args) {
regexTestPrint();
rexUtilTestForPrint();
oroTestForPrint();
}
/**
* this is to test the regex whici is supplied by the jdk1.4
*
*/
public static void regexTestPrint() {
Matcher testPreMatcher = Pattern.compile(rexPatternString)
.matcher(rexSearchString);
int groutCount = testPreMatcher.groupCount();
System.out.println(
"this is to print the group which is set by parenthesis :" +
groutCount);
if (testPreMatcher.find()) {
for (int i = 0; i < (groutCount + 1); i++) {
System.out.println(
"this is to print which the search of the group for the pattern " +
i + ":" + testPreMatcher.group(i));
}
}
}
/**
* This is to test fro print of the appache of rexUtil
*
*/
public static void rexUtilTestForPrint() {
RE re = new RE(rexPatternString);
boolean isTheRegMatch = re.match(rexSearchString);
if (isTheRegMatch) {
int matchCount = re.getParenCount();
System.out.println(
"This is to print which the search of the parenthesis count :" +
matchCount);
for (int mc = 0; mc < matchCount; mc++) {
System.out.println("The match content level is i=" + mc + ":" +
re.getParen(mc));
}
}
}
/**
* this is to test for the oro which appache supllying of regular
*
*/
public static void oroTestForPrint() {
PatternMatcher patternMatcher = new Perl5Matcher();
PatternCompiler patternCompiler = new Perl5Compiler();
org.apache.oro.text.regex.Pattern pattern;
MatchResult matchResult;
PatternMatcherInput patternMatcherInput;
try {
pattern = patternCompiler.compile(rexPatternString);
} catch (MalformedPatternException e) {
System.err.println("Bad pattern.");
System.err.println(e.getMessage());
return;
}
patternMatcherInput = new PatternMatcherInput(rexSearchString);
while (patternMatcher.contains(patternMatcherInput, pattern)) {
matchResult = patternMatcher.getMatch();
System.out.println("oro Result is group count :" +
matchResult.groups());
for (int k = 0; k < matchResult.groups(); k++) {
System.out.println("oro group Result fattern is " + k + "=:" +
matchResult.group(k));
}
}
}
}
下面是代码:大家应该都看得懂,全当是自己记录。
package simpleTest;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.regexp.RE;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Edgar(wxt)
*
*/
public class TestForRex {
static String rexPatternString = "^((http|https)://[^/]*/).*";
static String rexSearchString = "http://10.4.8.43/makdsm/amd.do";
/**
* @param args
*/
public static void main(String[] args) {
regexTestPrint();
rexUtilTestForPrint();
oroTestForPrint();
}
/**
* this is to test the regex whici is supplied by the jdk1.4
*
*/
public static void regexTestPrint() {
Matcher testPreMatcher = Pattern.compile(rexPatternString)
.matcher(rexSearchString);
int groutCount = testPreMatcher.groupCount();
System.out.println(
"this is to print the group which is set by parenthesis :" +
groutCount);
if (testPreMatcher.find()) {
for (int i = 0; i < (groutCount + 1); i++) {
System.out.println(
"this is to print which the search of the group for the pattern " +
i + ":" + testPreMatcher.group(i));
}
}
}
/**
* This is to test fro print of the appache of rexUtil
*
*/
public static void rexUtilTestForPrint() {
RE re = new RE(rexPatternString);
boolean isTheRegMatch = re.match(rexSearchString);
if (isTheRegMatch) {
int matchCount = re.getParenCount();
System.out.println(
"This is to print which the search of the parenthesis count :" +
matchCount);
for (int mc = 0; mc < matchCount; mc++) {
System.out.println("The match content level is i=" + mc + ":" +
re.getParen(mc));
}
}
}
/**
* this is to test for the oro which appache supllying of regular
*
*/
public static void oroTestForPrint() {
PatternMatcher patternMatcher = new Perl5Matcher();
PatternCompiler patternCompiler = new Perl5Compiler();
org.apache.oro.text.regex.Pattern pattern;
MatchResult matchResult;
PatternMatcherInput patternMatcherInput;
try {
pattern = patternCompiler.compile(rexPatternString);
} catch (MalformedPatternException e) {
System.err.println("Bad pattern.");
System.err.println(e.getMessage());
return;
}
patternMatcherInput = new PatternMatcherInput(rexSearchString);
while (patternMatcher.contains(patternMatcherInput, pattern)) {
matchResult = patternMatcher.getMatch();
System.out.println("oro Result is group count :" +
matchResult.groups());
for (int k = 0; k < matchResult.groups(); k++) {
System.out.println("oro group Result fattern is " + k + "=:" +
matchResult.group(k));
}
}
}
}