Java的正则表达式

package RegexTest;

/**
* Created by hu on 2016/3/29.
*/
/*
* Java的正则表达式
在正则表达式中,用\d表示一位数字,如果在其它语言中使用过正则表达式,
就会发现Java对反斜线\的不同处理。在其它语言中\\表示"想要在正则表达式中
插入一个普通的(字面上的)反斜线,请不要给它任何意义"。
而在Java中\\的意思是"想要插入一个正则表达式的反斜线,
所以其后面的字符具有特殊的意义"。例如,想要表达一位数字,
那么正则表达式应该为\\d。如果想要插入一个普通的反斜线,则应该是\\\\。
不过换行和制表符之类的东西只需要使用单反斜线:\n\t
* */
public class IntegerMatch {
public static void main(String[] args){
/*
*如果想要表达“一个或多个之前的字符”,应该使用"+"
*“前面可能有一个负号”,使用"-?",故?的意思是可能有也可能没有
* \\d表示后面跟的是数字
* */
System.out.println("-1234".matches("-?\\d+"));
System.out.println("5678".matches("-?\\d+"));
System.out.println("+911".matches("-?\\d+"));
//因为"+"在正则表达式中有特殊的意义,所以将其转义
System.out.println("+911".matches("(-|\\+)?\\d+"));
}
}

  

package RegexTest;

import java.util.Arrays;

/**
* Created by hu on 2016/3/29.
*/
/*
* String类自带了一个非常有用的正则表达式工具,split()方法,其功能是“将字符串从正则表达式匹配的地方切开”
* */
public class Splitting {
public static String knights="Then, when you have found the shrubbery, you must "
+"cut down the mightiest tree in the forest... "+"with ... a herring!";
public static void split(String regex){
System.out.println(Arrays.toString(knights.split(regex)));
}
public static void main(String[] args){
//按照空格来划分字符串
split(" ");
//\W的意思是非单词字符,\w则表示一个单词字符
split("\\W+");
//字母n后面跟着一个或多个非单词字符
split("n\\W+");
}
}

 

package RegexTest;

/**
* Created by hu on 2016/3/29.
*/
/*
* String.split()还有一个重载的版本,它允许限制字符串分割的次数
* String类自带的最后一个正则表达式工具是替换。你可以只替换正则表达式第一个匹配的子串,或是替换所有匹配的地方。
* */
public class Replacing {
public static String knights="Then, when you have found the shrubbery, you must "
+"cut down the mightiest tree in the forest... "+"with ... a herring!";
public static void main(String[] args){
//只替换第一个符合条件的子串,匹配以字母f开头,后跟多个字符
System.out.println(knights.replaceFirst("f\\w+","located"));
//替换所有符合条件的子串
System.out.println(knights.replaceAll("shrubbery|tree|herring","banana"));
}
}

  创建正则表达式:

Java的正则表达式

这只是常用的一部分

 

package RegexTest;

import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* Created by hu on 2016/3/29.
*/
/*
* 比起功能有限的String类,本人更喜爱构造更能强大的正则表达式对象。只需要导入java.util.regex包,
* 然后使用static Pattern.compile()方法来编译自己的正则表达式即可,它会根据String类型的正则表达式生成
* 一个Pattern对象,接下来把想要匹配的字符串输入到Pattern对象的matcher()方法。matcher()方法会生成一个Matcher
* 对象。
* */
public class TestRegularExpression {
public void testRegularExpression(String regex,String matchStr){
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(matchStr);
while (matcher.find()){
System.out.println("Match \""+ matcher.group()+"\"at position "+ matcher.start()+"-"+ (matcher.end()-1));
}
}
}

  

package RegexTest;

import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* Created by hu on 2016/3/29.
*/
public class Finding {
public static void main(String[] args){
Matcher m= Pattern.compile("\\w+").matcher("Evening is full of the linnet's wings");
while (m.find()){//find()像迭代器那样前向遍历输入的字符串
System.out.print(m.group() + " ");
}
System.out.println();
int i=0;
while (m.find(i)){//该find能够接收一个整数作为参数,该整数表示字符串中字符的位置,并以其作为搜索的起点。
System.out.print(m.group() + " ");
i++;
}
}
}

  其结果如下:

Java的正则表达式

上一篇:notepad++必读


下一篇:commonJS — 数字操作(for Number)