正则表达式的常用经典实例

总结了下常用的正则表达式用例,这个经常会用到,有些可能还没有,后边会继续更新,如果文章里有问题请大家指正

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

public class PatternTest {
	public static void main(String[] args) {
		//获取“.”以前的所有英文字符
		String TestStr = "请访问4555netpay.comjac.com/m?";
		String TestStrs = "dafafafafa";
		Pattern pattern = Pattern.compile("[a-zA-Z]+");
		int sep = TestStr.indexOf(".");
		String CutStr = null;
		if (sep >= 0) {
			CutStr = TestStr.substring(0,sep);
			Matcher mat = pattern.matcher(CutStr);
			if (mat.find()) {
				System.out.println(mat.group());
			}
		}
		// boolean IfWebsite = Pattern.matches("http://[\\w]+",TestStr);
		// System.out.print("IfWebsite = "+IfWebsite);
		System.out.println("CutStr = " + CutStr);
		//获取类似于这样的字符()
		Pattern p3 = Pattern
				.compile("(19|20)\\d\\d([- /.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])");
		Matcher m3 = p3
				.matcher("1900-01-01 2007/08/13 1900.01.01 1900 01 01 1900-01.01 1900 13 01 1900 02 31");
		while (m3.find()) {
			System.out.println(m3.group());
		}
        //获取字符串中的所有数字
		String TestStr1 = "+10010";
		Pattern pattern1 = Pattern.compile("[\\d]+");
		Matcher mat = pattern1.matcher(TestStr1);
		if (mat.find()) {
			System.out.println(mat.group());

		}
        //判断是否全为数字
		String strMobile = "+10010";
		if (strMobile.matches("\\d+")) {
			System.out.println("true");
		}

	}
}

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...