您的位置:首页 > 其它

正则表达式的基本知识

2016-12-10 15:12 323 查看
对于正则 我们以Java为例来说说

在学习正则前 首先必须要知道的就是转义字符以及使用正则的目的

一般来说我们大致把正则的功能分为四个

判断

分割

替换

获取

而关于这四个功能 Pattern和Matcher实现了 且一般这两个类都是配合使用的

其中的三个功能 判断 分割 替换在String类中都是实现的







我们先以String类说说它的这三个功能的使用吧!

String类的分割功能实现

public class split1 {
public static void main(String[] args) {
String t = "a\\b\\c\\d";
System.out.println("即将要分割的字符串:"+t);
String[] temp = t.split("\\\\");
System.out.println("我们以\\为分割点");
System.out.println("分割后的字符串数目:"+temp.length);
for(String str:temp){
System.out.println("分割结果:"+str);
}
}
}


运行结果

即将要分割的字符串:a\b\c\d
我们以\为分割点
分割后的字符串数目:4
分割结果:a
分割结果:b
分割结果:c
分割结果:d


这里理解了
\
这个转义字符的就没有什么问题的

不过值得注意的是下面这个例子

public class split1 {
public static void main(String[] args) {
String t = "\\a\\b\\c\\d";
System.out.println("即将要分割的字符串:"+t);
String[] temp = t.split("\\\\");
System.out.println("我们以\\为分割点");
System.out.println("分割后的字符串数目:"+temp.length);
for(int i=1;i<temp.length;i++){
System.out.println("第"+i+"个字符串:"+temp[i-1]);
}
}
}


运行结果

即将要分割的字符串:\a\b\c\d
我们以\为分割点
分割后的字符串数目:5
第1个字符串: 长度为:0
第2个字符串:a 长度为:1
第3个字符串:b 长度为:1
第4个字符串:c 长度为:1
第5个字符串:d 长度为:1


需要注意的是这个就了一个空串 以后要注意这个问题!

String类的判断功能实现

public class matcher1 {
public static void main(String[] args) {
String t = "I love java.java";
System.out.println("即将要判断的字符串:"+t);
String rex="I\\slove\\sjava\\.java";
System.out.println("匹配字符串:"+rex);
if(t.matches(rex)){
System.out.println("匹配成功!");
}else{
System.out.println("匹配失败!");
}
}
}


运行结果

即将要判断的字符串:I love java.java
匹配字符串:I\slove\sjava\.java
匹配成功!


像这种判断一整个字符串的正则可以加上
^
$


下面我会以Pattern和Matcher这两个类的判断为例子讲解

String类的替换功能实现

public class replaceAll1 {
public static void main(String[] args) {
String t = "I love java.java";
System.out.println("即将要替换的字符串:"+t);
String rex="\\w{4}";
System.out.println("要替换的字符串:"+rex);
System.out.println(t.replaceAll(rex,"*"));
}
}


运行结果

即将要替换的字符串:I love java.java
要替换的字符串:\w{4}
I * *.*


接下来就进行Pattern和Mather的介绍吧!

Pattern和Mather的判断功能

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

public class matcher1 {
public static void main(String[] args) {
String t = "Ilovejava.java";
System.out.println("即将要判断的字符串:"+t);
String rex="^\\w+\\.java$";//可能初学者不是很清楚 ^是行的开头 $是行的结尾 一般^和$是在匹配整个字符串才会写

System.out.println("匹配:"+rex);

Pattern pattern=Pattern.compile(rex);//匹配的
Matcher matcher=pattern.matcher(t);//要判断的字符串

if(matcher.matches()){
System.out.println("匹配成功");
}else{
System.out.println("匹配失败");
}
}
}


运行结果

即将要判断的字符串:Ilovejava.java
匹配字符串:^\w+\.java$
匹配成功


Pattern和Mather的替换功能

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

public class matcher1 {
public static void main(String[] args) {
String t = "hello Word 120";
System.out.println("即将要替换的字符串:"+t);
String rex="\\d+";
System.out.println("替换字符串:"+rex);

Pattern pattern=Pattern.compile(rex);
Matcher matcher=pattern.matcher(t);

System.out.println(matcher.replaceAll("*"));
}
}


运行结果

即将要替换的字符串:hello Word 120
替换字符串:\d+
hello Word *


Pattern和Mather的分割功能

以一个空格分割字符串

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

public class matcher1 {
public static void main(String[] args) {
String t = "hello Word 120";
System.out.println("即将要替换的字符串:"+t);
String rex="\\s";
System.out.println("分割字符串:"+rex);

Pattern pattern=Pattern.compile(rex);
Matcher matcher=pattern.matcher(t);

String [] result=pattern.split(t);

for(String str:result){
System.out.println(str);
}
}
}


运行结果

即将要分割的字符串:hello Word 120
分割字符串:\s
hello
Word
120


Pattern和Mather的获取功能

找出字符串中的出现的数字

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

public class matcher1 {
public static void main(String[] args) {
String t = "hello Word 120 hello Word 110";
System.out.println("即将要获取的字符串:"+t);
String rex="\\d+";
System.out.println("获取字符串:"+rex);

Pattern pattern=Pattern.compile(rex);
Matcher matcher=pattern.matcher(t);

while(matcher.find()){
System.out.println(matcher.group());
}
}
}


即将要获取的字符串:hello Word 120 hello Word 110
获取字符串:\d+
120
110


当然以上全是正则表达式的基础的基础!在正则表达式中,或许有些表达式很长,但也不要方,慢慢来搞几天,你就会发现很简单的,对于正则仅仅就是记得东西比较多 而且正则表达式是作为程序猿必须搞懂 弄懂的知识之一

END!!!!!!!!!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: