ITeye博客

正则表达式统计字符串出现的次数

ChineseThink · 2012-06-07 08:10 · Java · 编程语言 · Markdown

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

public class RegexExpression {

	public static void main(String[] args) {
		//测试数据
		String str = "fsdfeofkldfleierueo";
                //e表示需要匹配的数据,使用Pattern建立匹配模式
		Pattern p = Pattern.compile("e");
                //使用Matcher进行各种查找替换操作
		Matcher m = p.matcher(str);
		int i = 0;
		while(m.find()){
			i++;
		}
		
		System.out.println(i);
		
	}

}

正则表达式几乎在各种语言中都有实现,特别是对于做一些简单的数据校验来说很方便很实用。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。