【HDU2279】File Search Tool(JAVA正则匹配)

Categories: 数据结构和算法
Tags: ,
Comments: No Comments
Published on: 2011 年 05 月 17 日

WisKey downloaded much software in winter vacation, and the disk was in a state of confusion. He wastes many times to find the file everyday. So he wants a tool to help him do this work.
The file name consists of lowercase letters.
The name pattern is a string of lowercases, '?'s and '*'s. In a pattern, a '?' matches any single lowercase, and a '*' matches none or more lowercases.
Let’s do this~
题意:给你一些文件名字,全部是小写,?号能够匹配任何一个小写字母,*号能够匹配0个或多个字符,求各种匹配的个数。
题解:用JAVA的正则偷偷的水过……详情见代码,十分简单。

import java.io.*;
import java.util.regex.*;
import java.util.*;
public class Main{
	public static void main(String args[]) throws Exception
	{
		
		String str[]=new String[10005];
		String temp;
		//Pattern p=Pattern.compile(regEx);
		Scanner cin=new Scanner(System.in);
		while(cin.hasNext()){
		int a=cin.nextInt(),b=cin.nextInt();
		for(int i=0;i<a;i++)
		{
			str[i]=cin.next();
		}
		for(int i=0;i<b;i++)
		{
			int res=0;
			temp=cin.next();
			String ss="^";
			for(int k=0;k<temp.length();k++)
			{
				if(temp.charAt(k)=='?') ss+='.';
				else if(temp.charAt(k)=='*') ss+=".*";
				else ss+=temp.charAt(k);
			}
			ss +="$";
			Pattern p=Pattern.compile(ss); 
			//System.out.println(temp);
			for(int k=0;k<a;k++)
			{
				
				Matcher m=p.matcher(str[k]); 
				boolean rs=m.find(); 
				if(rs) res++;
				//System.out.println(ss+res+str[k]);
			}
			if(res==0) System.out.println("Not match");
			else System.out.println(res);
		}	
	}
		}
}

我猜你可能也喜欢:

No Comments - Leave a comment

Leave a comment

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Welcome , today is 星期五, 2017 年 12 月 15 日