JavaLearning:正则表达式 Pattern类 & Matcher类

前端之家收集整理的这篇文章主要介绍了JavaLearning:正则表达式 Pattern类 & Matcher类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package org.fun.classdemo;

import java.util.regex.Pattern;

public class PatterinDemo {

	public static void main(String[] args) {
		Pattern pat = Pattern.compile("\\d+");
		String str = "afde78555554774fg44";
		String s[] = pat.split(str);
		for (int x = 0; x < s.length; x++) {
			System.out.println(s[x] + ",");
		}
	}
}

package org.fun.classdemo;

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

public class MatcherDemo01 {

	public static void main(String[] args) {
		String str = "111-22-333";
		String pat = "\\d{3}-\\d{2}-\\d{3}";
		Pattern p = Pattern.compile(pat);
		Matcher m = p.matcher(str);
		if (m.matches()) {
			System.out.println("success");
		} else {
			System.out.println("failure");
		}
	}
}

package org.fun.classdemo;

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

public class MatcherDemo02 {

	public static void main(String[] args) {
		String str = "df55gg33366gdff";
		String pat = "\\d+";
		Pattern p = Pattern.compile(pat);
		Matcher m = p.matcher(str);
		System.out.println(m.replaceAll("*"));
	}
}
原文链接:/regex/361467.html

猜你在找的正则表达式相关文章