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 7862415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:07:16+00:00 2026-06-02T23:07:16+00:00

I have some files to scan with patterns using preg_match like: File name: (a

  • 0

I have some files to scan with patterns using preg_match like:

File name:

(a group: one)
one.txt 

(another group: one-aaa)
one-aaa.txt
one-aaa_1.txt
one-aaa_b.txt
one-aaa_3.txt
one-aaa_whatever.txt

(some other group: one-bbb)
one-bbb.jpg
one-bbb_1.txt
one-bbb_2.txt
one-bbb_t.txt
one-bbb_whatever.txt

The group is defined by names (hence: one, one-aaa, one-bbb are different groups), and limited to file .txt.

Please do not suggest to use different directories. Those files are already scattered at some directories, I need a way to find matches by keyword, not directories.

Now I can define groups manually by specifying “one”, “one-aaa”, etc, but have trouble with preg_match. My preg_match returns “one” and “one-aaa” as a single group:

$keyword = 'one';
$match = '/(^)' . $keyword . '(.*\.txt$)/';

$match = '/\b(' . $keyword . ')\b(.*\.txt$)/';

The expected return:
one.txt

Unexpected returns:
one.txt
one-aaa.txt, etc

UPDATE 1:
When keyword changed to ‘one-aaa’, I want it return: one-aaa.txt, one-aaa_1.txt, and the likes.
The way I group is:

$keyword = str_replace('_', ' ', $file->name);
returns: one, one-aaa, one-bbb, etc

What I want to say in plain English:

  1. find matches that start with “one”, returns: one_1.txt, one_2.txt
  2. find matches that start with “one-aaa”, returns: one-aaa_1.txt, one-aaa_2.txt, etc

Can anyone shed the light on the correct regex?

Thanks

UPDATE 2:
Somebody here previously provided suggestion to avoid greedy regex, and use .*? instead, but the answer was deleted. It finally works this way as per his suggestion:

$match = '/^\b(' . $keyword . ')\b(.*?.txt$)/';

Who should I assign an answer to now? Can anyone volunteer to write a working answer like above, or a betterment of it?

UPDATE 3:
Oops, I talked too soon. It didn’t work, but the key was reset somewhow when I change key|value pairs thats why I lost track of the double inclusion. Sorry the above still no go.

UPDATE 4:
I finally made it with additional condition to simply exclude the output, if they don’t match the group. Extra codes and also extra scanning, bad, but at least it works as expected by now. Still using the suggested regex above.
Still looking for ultimate regex solution, if any. If no, then “no” should be the chosen answer

Thanks

  • 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-02T23:07:19+00:00Added an answer on June 2, 2026 at 11:07 pm

    Of course – the “.*” lets those other characters in. change it to:

    $keyword = 'one';
    $match = '/(^)' . $keyword . '(\.txt$)/';
    
    $match = '/\b(' . $keyword . ')\b(\.txt$)/';
    

    “.*” means any character, appearing 0 times or more…

    edit:

    After seeing your updates, assuming one_10 or one_100 can also exist.

    You can try: $match = '/^' . $keyword . '(_[0-9]+)?\.txt$/';

    This means that after the keyword may come an underscore with a number following.

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

Sidebar

Related Questions

i have some files located in c:\MyApp\file.txt and current executing assenbly in in debud
I have some files that are uuencoded, and I need to decode them, using
I have some files across several folders: /home/d/folder1/a.txt /home/d/folder1/b.txt /home/d/folder1/c.mov /home/d/folder2/a.txt /home/d/folder2/d.mov /home/d/folder2/folder3/f.txt How
I have some files that I'd like to delete the last newline if it
I have some files in my SVN repository that I would like to have
So I have lots of links like address:port/Bla-bla/bla-bla/file/blabla234times/ created by some server (like VLC)
I'm currently using the following code to scan files that have been uploaded as
We have some files on our website that users of our software can download.
I have some files stored at amazon. all in private mode, and since I
I have some files stored in a database blob column in Oracle 9. I

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.