Let’s suppose we want to match files with the extensions .txt
, .csv
, and .log
. We’ll use the java.util.regex
package to accomplish this task.
First, we need to define the regular expression pattern for matching file extensions. The pattern can be constructed using the pipe (|
) character, which acts as an OR operator, allowing us to match multiple patterns in a single expression.
String fileExtensionPattern = "\\.txt|\\.csv|\\.log";
In the above code snippet, we’ve escaped the dot character (.
) using a backslash (\
) since the dot has a special meaning in regular expressions.
To match a file against this pattern, we can use the Pattern
and Matcher
classes from the java.util.regex
package.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class FileExtensionMatcher {
public static void main(String[] args) {
String[] fileNames = {"file1.txt", "file2.csv", "file3.log", "file4.jpg"};
String fileExtensionPattern = "\\.txt|\\.csv|\\.log";
Pattern pattern = Pattern.compile(fileExtensionPattern);
for (String fileName : fileNames) {
Matcher matcher = pattern.matcher(fileName);
if (matcher.find()) {
System.out.println(fileName + " is a valid file");
} else {
System.out.println(fileName + " is not a valid file");
}
}
}
}
In the code snippet above, we define an array of file names fileNames
that we want to match against the file extension pattern. We compile the pattern using Pattern.compile()
and then iterate over each file name.
For each file name, we create a Matcher
using the pattern and use the find()
method to check if the pattern matches any part of the file name. If it does, we output that it’s a valid file; otherwise, we output that it’s not a valid file.
When you run the above code, you will get the following output:
file1.txt is a valid file
file2.csv is a valid file
file3.log is a valid file
file4.jpg is not a valid file
By using regular expressions, we can easily match specific file extensions in a given text or file name. This can be highly useful when filtering files based on their extensions or performing any other file-related operations.
Remember to import the java.util.regex.Pattern
and java.util.regex.Matcher
classes before using regular expressions.
#Java #RegularExpressions