2013年12月12日星期四

java regex

I wrote a regular expression , which determines whether the string contains " _4 digits . Csv" string , but if the characters found containing " _5 numbers . Csv" , the program runs very slowly , please help me look , I do not have much experience of regular expressions , poorly written , if you can optimize it, please help me to optimize what bigwigs .


import java.util.regex.Pattern;

public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "20131210_19045.csv,a.csv,";
System.out.println(""+Pattern.matches("(\\w*?\\W*?)*?(\\w?_\\d{4}.csv,)+(\\w*?\\W*?)*?", str));
}

}

------ Solution ------------------------------------- -------
try this
			String str = "20131210_19015.csv,a.csv,";
System.out.println(""+Pattern.matches(".*(?<=_)(\\d{4})(?=.csv).*", str));

------ Solution ------------------------------------- -------
. *? _ \ \ d {4} \ \. csv. *
------ Solution -------------- ------------------------------

String str = "20131210_19045.csv,a.csv,";       
System.out.println(str.matches(".*_\\d{4}\\.csv.*"));

------ Solution ---- ----------------------------------------
	@Test
public void test()
{
String str = "20131210_19015.csv,a.csv,";    
System.out.println(""+Pattern.matches(".*_\\d{4,}+\\.csv.*", str));
}


Although successful , but the feeling is still slow. . .

没有评论:

发表评论