。。。。大数据工程师必备技能图谱
正则表达式: 其实一种规则,有自己特殊的应用,其作用就是针对于字符串进行操作。
正则: 就是用于操作字符串的规则,其中这些规则使用了一些字符表示。
1. 正则表达式符号
1.1 预定义字符类 (任何预定字符没有加入数量词之前只能匹配一个字符)
System.out.println("a".matches("."));
System.out.println("1".matches("\\d"));
System.out.println("%".matches("\\D"));
System.out.println("\r".matches("\\s"));
System.out.println("^".matches("\\S"));
System.out.println("a".matches("\\w"));
1.2 Greedy 数量词

System.out.println( "a".matches(".") );
System.out.println( "a".matches("a") );
System.out.println("a".matches("a?") );
System.out.println( "aaa".matches("a*") );
System.out.println( "".matches("a+") );
System.out.println( "aaaaa".matches("a{5}") );
System.out.println( "aaaaaaaaa".matches("a{5,8}") );
System.out.println( "aaa".matches("a{5,}") );
System.out.println( "aaaaab".matches("a{5,}") );
1.3 范围表示

System.out.println( "a".matches("[a]") );
System.out.println( "aa".matches("[a]+") );
System.out.println( "abc".matches("[abc]{3,}") );
System.out.println( "abc".matches("[abc]+") );
System.out.println( "dshfshfu1".matches("[^abc]+") );
System.out.println( "abcdsaA".matches("[a-z]{5,}") );
System.out.println( "abcdsaA12".matches("[a-zA-Z]{5,}") );
System.out.println( "abcdsaA12".matches("[a-zA-Z0-9]{5,}") );
System.out.println( "abdxyz".matches("[a-c[x-z]]+"));
System.out.println( "bcbcbc".matches("[a-z&&[b-c]]{5,}"));
System.out.println( "tretrt".matches("[a-z&&[^b-c]]{5,}"));
2 匹配功能
需求:校验QQ号,要求:必须是5~15位数字,0不能开头。public static void checkQQ2()
{
String qq = "12345";
String reg = "[1-9][0-9]{4,14}";
boolean b = qq.matches(reg);
System.out.println("b="+b);
}
[1-9][0-9]{4,14} 解析: [1-9]表示是第一位数字是会出现1-9范围之间的其中一个,下来的数字范围会出现在0-9之间,至少出现4次,最多出现14次。
3. 切割功能
3.1 根据空格对一段字符串进行切割
public static void splitDemo()
{
String str = "aa.bb.cc";
str = "-1 99 4 23";
String[] arr = str.split(" +");
for(String s : arr) // 相当于 for(i=0,i<arr.length,i++), 用字符串s去输出arr,直到arr的长度减到0;
{
System.out.println(s);
}
}
3.2 根据重叠词进行切割 (将重叠词去掉)
public static void splitDemo2()
{
String str = "sdqqfgkkkhjppppkl";
String[] arr = str.split("(.)\\1+"); // 任意字符 . 正则的内容要被复用,那么需要对正则的内容进行分组,
// 用()表示分组,组号从1开始
for(String s : arr)
{
System.out.println(s);
}
}

注意:为了提高规则复用,用()进行封装,每一个括号都有一个编号,从1开始,为了复用这个规则。可以通过编号来完成该规则的调用。需要对编号数字进行转义。\\1就代表获取1组规则。
4 替换功能
public static void repla() {
String str = "如有需求请联系我:1234567890如有需求请联系我:1234567890如有需求请联系我:1234567890如有需求请联系我:1234567890"
+"如有需求请联系我:1234567890如有需求请联系我:1234567890如有需求请联系我:1234567890如有需求请联系我:1234567890";
String reg = "1[2345]\\d{8}";
str = str.replaceAll(reg, "*****");
System.out.println(str);
}
public static void repla2() {
String str = "我我我我我要要要做做想想想木木";
// 如果需要在replaceAll方法正则的外部引用组的内容,要使用"$组号"
str = str.replaceAll("(.)\\1+", "$1");
System.out.println(str);
}
如果需要在replaceAll方法正则的外部引用组的内容,要使用 "$组号"
5 获取
获取需要使用到正则的两个对象:使用的是用正则对象Pattern 和匹配器Matcher。
用法:
范例:
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
步骤:
1,先将正则表达式编译成正则对象。使用的是Pattern类一个静态的方法。compile(regex);
2,让正则对象和要操作的字符串相关联,通过matcher方法完成,并返回匹配器对象。
3,通过匹配器对象的方法将正则模式作用到字符串上对字符串进行针对性的功能操作
find() 尝试查找与该模式匹配的输入序列的下一个子序列 ,布尔型
group() 返回由以前匹配操作所匹配的输入子序列。
注意: 使用group()方法时,一定要用find()提前查找到
实例一:获取由3个字母组成的单词
public class test_1{
public static void main(String args[]) {
// 找出3个字母的单词
String content = "wo shi xiao xian nv wo shi nv xia";
String reg = "\\b[a-zA-Z]{3}\\b";
// 先把字符串编译成pattern对象
Pattern p = Pattern.compile(reg);
// 使用正则对象匹配字符串用于产生一个Matcher对象
Matcher m = p.matcher(content);
// System.out.println("有符合规则的字符串吗?" + m.find());
// System.out.println("获取结果: " + m.group());
while(m.find()) {
System.out.println("获取结果: " + m.group());
}
} }
replaceAll(String regex, String replacement) : 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
String reg = "\\b[a-zA-Z]{3}\\b";
\b 表示单词边界 (单词的开始和结束,不匹配任何字符)
实例二: 找出其中的邮箱
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test_1{
public static void main(String args[]) {
String str = "如有需求请联系我:1234567@qq.com如有需求请联系我:1234567@qq.com.cn如有需求请联系我:1234567@qq.com如有需求请联系我:1234567@qq.com"
+"如有需求请联系我:abcdefg@163.net如有需求请联系我:1234567@163.com.cn如有需求请联系我:1234567@163.com.cn如有需求请联系我:1234567@qq.com";
String reg = "[a-zA-Z1-9]\\w{5,17}@[a-zA-Z0-9]{2,}(\\.(cn|com|net)){1,2}";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println(m.group());
}
}
}