Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:10:14+00:00 2026-06-12T16:10:14+00:00

i wanted to write a file search program where the user can enter the

  • 0

i wanted to write a file search program where the user can enter the serach pattern(any valid regex) and the file name matching the same will be returned.

e.g MFile123.tx will find UMFile123.txt and AIIMFile123.txs

I tried the following, but it did not work:

import java.util.regex.*;
public class regexTest {
    public static void main(String... a){
        String file="UMFile123.txt";
       //String pattern="*MFile*.tx?"; TRIED with \*MFile*.tx , but no use
        String pattern="UMFile*.tx?";
        Pattern p=Pattern.compile(pattern);
        Matcher m=p.matcher(file);
        if(m.matches()){
            System.out.println("Hi!it matches!!");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:10:15+00:00Added an answer on June 12, 2026 at 4:10 pm

    The simplest solution would be to “quote” or “escape” the user entered file name/pattern and prepend & append a .*? to it. Something like:

    final String userFname = "MFile123.tx";
    final String escapedName = Pattern.quote(userFname);
    final String fnamePattern = ".*?" + escapedName + ".*";
    final Pattern pat = Pattern.compile(fnamePattern);
    final Matcher m = pat.matcher(file);
    

    I would rather not go with the business of manual escaping as mentioned by the other answers here. Tricky to get right IMHO. If you want case-insensitive match, just throw in one of the insensitive regex flags and you should be good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge text file and I wanted to write a program which
I wanted to write a small script that searched for an exact file name,
I wanted to write a quick program to get the file organization of my
I wanted to write a regex to count the number of spaces/tabs/newline in a
Just wanted to write some recursion but can't check if the child is in
I wanted to write a program that test if two files are duplicates (have
I wanted to write a function for grabbing all tweets for specified user, but
I wanted to write a C code to zip a file in unix. I
I wanted to write a makefile for a program that has source files located
I have a user registration form, and wanted to write a human detecting captcha

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.