正则表达式第四篇(Java中使用正则表达式)

前端之家收集整理的这篇文章主要介绍了正则表达式第四篇(Java中使用正则表达式)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


1、常用

	// 以多条件分割字符串时:以逗号(,)或者号(|)来分隔字符串
	public static void fun2() {
		Pattern pattern = Pattern.compile("[,|]");
		String[] strs = pattern.split("Java Hello World  Java,Hello,World|Sun");
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}

	}

	// 以多条件分割字符串时:以逗号(空格)(,)或者号(|)来分隔字符串
	public static void fun3() {
		Pattern pattern = Pattern.compile("[,|]");
		String[] strs = pattern.split("Java Hello  World  Java,World|Sun");
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
	}

	// 以多条件分割字符串时:以逗号(空格)(,)或者号(|)来分隔字符串
	public static void fun4() {
		Pattern pattern = Pattern.compile("[,|]+"); /* +表示分隔符可以连续出现算一次分隔 */
		String[] strs = pattern.split("Java Hello World  Java,World|Sun");
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
	}

	// 文字替换(首次出现字符)
	public static void fun5() {
		Pattern pattern = Pattern.compile("正则表达式");
		Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
		// 替换第一个符合正则的数据
		System.out.println(matcher.replaceFirst("Java"));
	}

	// 文字替换(全部)
	public static void fun6() {
		Pattern pattern = Pattern.compile("正则表达式");
		Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
		// 替换第一个符合正则的数据
		System.out.println(matcher.replaceAll("88888888888888"));
	}

	// 文字替换(置换字符)
	public static void fun7() {
		Pattern pattern = Pattern.compile("正则表达式");
		Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World ");
		StringBuffer sbr = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sbr,"Java");
		}
		matcher.appendTail(sbr);
		System.out.println(sbr.toString());
	}

	// 去除html标记
	public static void fun8() {
		Pattern pattern = Pattern.compile("<.+?>",Pattern.DOTALL);
		Matcher matcher = pattern.matcher("<a href=\"index.html\">主页</a>");
		String string = matcher.replaceAll("");
		System.out.println(string);
	}

	// 查找html中对应条件字符串
	public static void fun9() {
		String str1 = "href=\"(.+?)\""; // .+?:表示1个以上任意字符并且可以出现多次。并且外面有""号。
		Pattern pattern = Pattern.compile(str1);
		Matcher matcher = pattern.matcher("<a href=\"index.html\">主页</a>");
		if (matcher.find())
			System.out.println(matcher.group(1));
	}

	public static void main(String[] args) {
		fun2();
		System.out.println("2-------------------------------");
		fun3();
		System.out.println("3-------------------------------");
		fun4();
		System.out.println("4-------------------------------");
		fun5();
		System.out.println("5-------------------------------");
		fun6();
		System.out.println("6-------------------------------");
		fun7();
		System.out.println("7-------------------------------");
		fun8();
		System.out.println("8-------------------------------");
		fun9();
	}


2、替换指定{}中文
public class ReplacePath {

	public static String replace(final String sourceString,Object[] object) {
		String temp = sourceString;
		for (int i = 0; i < object.length; i++) {
			String[] result = (String[]) object[i];
			Pattern pattern = Pattern.compile(result[0]);
			Matcher matcher = pattern.matcher(temp);
			temp = matcher.replaceAll(result[1]);
		}
		return temp;
	}

	public static void main(String[] args) {
		String str = "Java目前的发展史是由{0}年-{1}年";
		String[][] object = { 
				new String[] { "\\{0\\}","1995" },new String[] { "\\{1\\}","2007" } 
		};
		System.out.println(replace(str,object));
	}
}


3、以正则条件查询指定目录下文件

public class FilesAnalyze {
	private ArrayList<File> files = new ArrayList<File>(); // 用于承载文件路径
	private String _path; // 用于承载未合并的正则公式
	private String _regexp;

	// 解析输入流
	FilesAnalyze(String path,String regexp) {
		getFileName(path,regexp);
	}

	class MyFileFilter implements FileFilter {
		public boolean accept(File file) {// 匹配文件名称
			try {
				Pattern pattern = Pattern.compile(_regexp);
				Matcher match = pattern.matcher(file.getName());
				return match.matches();
			} catch (Exception e) {
				return true;
			}
		}
	}

	// 分析文件名并加入files
	private void getFileName(String path,String regexp) { // 目录
		_path = path;
		_regexp = regexp;
		File directory = new File(_path);
		File[] filesFile = directory.listFiles(new MyFileFilter());
		if (filesFile == null)
			return;

		for (int j = 0; j < filesFile.length; j++) {
			files.add(filesFile[j]);
		}

		return;
	}

	// 显示输出信息 
	public void print(PrintStream out) {
		Iterator<File> elements = files.iterator();
		while (elements.hasNext()) {
			File file = (File) elements.next();
			out.println(file.getPath());
		}
	}

	public static void output(String path,String regexp) {
		FilesAnalyze fileGroup1 = new FilesAnalyze(path,regexp);
		fileGroup1.print(System.out);
	}

	public static void main(String[] args) {
		output("C:\\新建文件夹\\","[A-z|.]*"); // 是英文文件
	}
}
原文链接:https://www.f2er.com/regex/361345.html

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