Analysis of conditional query for Java regular expression implementation

Source: Internet
Author: User
Tags regular expression hosting

How does a Java regular expression implement a conditional query instance? We are in the actual development process will encounter such a real demand, then we look at the specific implementation process, I hope to help you.

Java Regular Expression Implementation condition query instance:

//java The requirement for a regular expression to implement a conditional query: To query a regular condition for a file in a specified directory

//For caching a list of files
Private ArrayList files = new ArrayList ();
//With To the hosting file path
private string _path;
//For hosting unincorporated regular formulas
private string _regexp;

Class Myfilefilter implements FileFilter {

/**
* Matching file name
*/
Public boolean accept (F    ile file) {
Try {
Pattern pattern = pattern.compile (_regexp);
Matcher match = Pattern.matcher (File.getname ());
return match.matches ();
} catch (Exception e) {
return true;
}
}


/**
* Parse input stream
* @param inputs
*/
Filesanalyze (String path,string regexp) {
GetFileName (PATH,REGEXP);
}
//java A regular expression implementation condition query
/**
* Parse file name and add files
* @param input
*/
private void GetFileName (Stri    ng path,string regexp) {
//directory
_path=path;
_regexp=regexp;
File directory = new file (_path);
file[] Filesfile =
Directory.listfiles (new Myfilefilter ());br> if (filesfile = null) return;   
for (int j = 0; J < Filesfile.length; J + +) {
Files.add (filesfile[j]);
}
return;
}

/**
* Display output Information
* @param out
*/
public void print (PrintStream out) {
iterator Elem Ents = Files.iterator ();
while (Elements.hasnext ()) {
File file= (file) Elements.next ();
Out.println (File.getpath ());
}
}
//java A regular expression implementation condition query
public static void output (
String path,string regexp) {

Filesanalyze Filegro Up1 =
New Filesanalyze (PATH,REGEXP);
Filegroup1.print (System.out);
}

public static void Main (string[] args) {
output ("c:\\", "[a-z|.]" *");
}

There are a lot of Java regular functions, in fact, as long as the character processing, there is no regular things do not exist.

Java Regular Expressions implementation of conditional query related operations to introduce you here, I hope that you understand and learn Java regular expressions to implement conditional query help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.