1.量词Quantifier
a.作用:限定之前的字符出现的次数
b.形式:
1.*:之前的字符可以出现0次到无穷多次。
2.+:之前的字符至少需要出现1次。
3.?:之前的字符至多只能出现1次。
2.区间量词
a.作用:具体规定字符的出现次数
b.形式:
1.{min,max}
2.{min, }
3.{number}
4.*={0,}
5.+={1,}
6.?={0,1}
7.用\\d+验证数字字符串。
3.量词的局限
a.量词只能规定之前字符或字符组的出现次数
b.如果要规定一个字符串的出现次数,必须使用括号(...),在括号内填写字符串,在闭括号之后添加量词。
4.括号的用途:多选结构
a.字符组(这就是字符组[1234567890])只能表示某个位置可能出现的单个字符,而不能表示某个位置可能出现的字符串。
b.作用:表示某个位置可能出现的字符串
c.形式:
1.(...|...),在竖线两端添加各个字符串
2.(...|...|...|...)
5.括号的用途:捕获分组
a.作用:将括号内的子表达式捕获的字符串存放到匹配结果中,供匹配完成后访问
a.只要使用了括号,就存在捕获分组
b.捕获分组按照开括号出现的从左至右的顺序编号,遇到括号嵌套的情况也是如此。
a.如果正则表达式很复杂,或者需要处理的文本很长,捕获分组会降低效率。
b.作用:仅仅用来对表达式分组,而不把分组捕获的文本存入结果。
a.作用:在表达式的某一部分,动态重复之前的子表达式所匹配的文本。
a.作用:限定之前的字符出现的次数
b.形式:
1.*:之前的字符可以出现0次到无穷多次。
2.+:之前的字符至少需要出现1次。
3.?:之前的字符至多只能出现1次。
2.区间量词
a.作用:具体规定字符的出现次数
b.形式:
1.{min,max}
2.{min, }
3.{number}
4.*={0,}
5.+={1,}
6.?={0,1}
7.用\\d+验证数字字符串。
3.量词的局限
a.量词只能规定之前字符或字符组的出现次数
b.如果要规定一个字符串的出现次数,必须使用括号(...),在括号内填写字符串,在闭括号之后添加量词。
4.括号的用途:多选结构
a.字符组(这就是字符组[1234567890])只能表示某个位置可能出现的单个字符,而不能表示某个位置可能出现的字符串。
b.作用:表示某个位置可能出现的字符串
c.形式:
1.(...|...),在竖线两端添加各个字符串
2.(...|...|...|...)
5.括号的用途:捕获分组
a.作用:将括号内的子表达式捕获的字符串存放到匹配结果中,供匹配完成后访问
b.形式:使用普通的括号(...)
/**
* 括号的用途:捕获分组
* */
public class CaptureBasic {
public static void main(String[] args) {
String email = "webmaster@itcast.net";
String regex = "(\\w+)@([\\w.]+)";
//String regex = "(\\w+)@([\\w]+.[\\w]+)";//比较一下这种情况
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(email);
if (m.find()) {
//组零表示整个模式,因此表达式 m.group(0) 等效于 m.group()。
System.out.println("email add is:\t" + m.group(0));
System.out.println("username is:\t" + m.group(1));
System.out.println("hostname is:\t" + m.group(2));
//System.out.println("exception is:\t" + m.group(3));
}
}
}
6.捕获分组的注意事项
a.只要使用了括号,就存在捕获分组
b.捕获分组按照开括号出现的从左至右的顺序编号,遇到括号嵌套的情况也是如此。
c.如果捕获分组之后存在量词,则匹配结果中,捕获分组保存的是子表达式最后一次匹配的字符串。------注意掌握!
/**
* 组的使用......掌握!!!
* */
public class CaptureMind {
public static void main(String[] args) {
explainGroupNo();
System.out.println("");
explainGroupQuantifier();
}
/**
* 第一个左括号是第一组...依此类推。
* */
public static void explainGroupNo() {
String email = "webmaster@itcast.net";
String regex = "((\\w+)@([\\w.]+))";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(email);
if (m.find()) {
System.out.println("match result:\t" + m.group(0));
System.out.println("group No.1 is:\t" + m.group(1));
System.out.println("group No.2 is:\t" + m.group(2));
System.out.println("group No.3 is:\t" + m.group(3));
}
}
/**
* 与上面方法中正则表达式的区别<br>
* 如果捕获分组之后存在量词,则匹配结果中,捕获分组保存的是子表达式最后一次匹配的字符串。------注意掌握!
* */
public static void explainGroupQuantifier() {
String email = "webmaster@itcast.net";
String regex = "(\\w)+@([\\w.])+";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(email);
if (m.find()) {
System.out.println("match result:\t" + m.group(0));
System.out.println("group No.1 is:\t" + m.group(1));
System.out.println("group No.2 is:\t" + m.group(2));
}
}
}
7.不捕获文本的括号
a.如果正则表达式很复杂,或者需要处理的文本很长,捕获分组会降低效率。
b.作用:仅仅用来对表达式分组,而不把分组捕获的文本存入结果。
c.形式:(?:...)
/**
* 不捕获文本的括号,形式:(?:...)------掌握!!!
* */
public class NoCaptureParenthese {
public static void main(String[] args) {
String email = "webmaster@itcast.net";
String regex = "(?:webmaster|admin)@(itcast.net)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(email);
if (m.find()) {
System.out.println("match result:\t" + m.group(0));
System.out.println("group No.1 is:\t" + m.group(1));
}
}
}
8.括号的用途:反向引用
a.作用:在表达式的某一部分,动态重复之前的子表达式所匹配的文本。
b.形式:\1
/**
*括号的用途:反向引用
*a.作用:在表达式的某一部分,动态重复之前的子表达式所匹配的文本。
*b.形式:\1
* */
public class BackReferenceBasic {
public static void main(String[] args) {
String[] strings = new String[] { "<h1>good</h1>", "<h1>bad</h2>"};
String regex = "<(\\w+)>[^<]+</(\\1)>";//掌握!!!
for (String str : strings) {
if (str.matches(regex)) {
System.out.println("\"" + str
+ "\" can be matched with regex \"" + regex
+ "\"");
} else {
System.out.println("\"" + str
+ "\" can not be matched with regex \"" + regex
+ "\"");
}
}
}
}
研究下面一个问题:
public class BackReferenceReplace {
public static void main(String[] args) {
String dupWords = "word word";
String dupWordRegex = "(\\w+)\\s+(\\1)";
//以下两行是扩充的代码
//String dupWords = "word word word word";
//String dupWordRegex = "(\\w+)(\\s+(\\1))+";
System.out.println("Before replace:\t" + dupWords);
//根据java语句的规范,"$1"表示的是正则表达式中编号为1的分组所捕获的字符串......理解!!!
System.out.println("After replace:\t"
+ dupWords.replaceAll(dupWordRegex, "$1"));
}
}
获取文本中所有的是数字
public static void main(String[] args) {
String toStr = "";
String fromStr = "商品名称";
String infoStr = "商品名称:2072万金钱=50元 QQ三国问鼎(网通)三足鼎立 低价便宜";
if(infoStr.contains(fromStr)){
toStr = infoStr.substring(infoStr.indexOf(":")+1);
}
String regex = "\\d+";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(toStr);
while(m.find()){
System.out.println(m.group());
}
//System.out.println(toStr);
}
java使用正则表达式获取字符串中的所有英文单词或数字
package regex;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test{
public static void main(String[] args){
String str="SUN公司被Oracle收购,是否意味着java被逼上了死路?";
String s = "\\d+.\\d+|\\w+";
Pattern pattern=Pattern.compile(s);
Matcher ma=pattern.matcher(str);
while(ma.find()){
System.out.println(ma.group());
}
}
}