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

  • Home
  • SEARCH
  • 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 125991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:14:48+00:00 2026-05-11T05:14:48+00:00

How can I fix this RegEx to optionally capture a file extension? I am

  • 0

How can I fix this RegEx to optionally capture a file extension?

I am trying to match a string with an optional component, but something appears to be wrong. (The strings being matched are from a printer log.)

My RegEx (.NET Flavor) is as follows:

.*(header_\d{10,11}_).*(_.*_\d{8}).*(\.\w{3,4}).* ------------------------------------------- .*                   # Ignore some garbage in the front (header_             # Match the start of the file name,     \d{10,11}_)      #     including the ID (10 - 11 digits) .*                   # Ignore the type code in the middle (_.*_\d{8})          # Match some random characters, then an 8-digit date .*                   # Ignore anything between this and the file extension (\.\w{3,4})          # Match the file extension, 3 or 4 characters long .*                   # Ignore the rest of the string 

I expect this to match strings like:

str1 = 'header_0000000602_t_mc2e1nrobr1a3s55niyrrqvy_20081212[1].doc [Compatibility Mode]' str2 = 'Microsoft PowerPoint - header_00000000076_d_al41zguyvgqfj2454jki5l55_20071203[1].txt' str3 = 'header_00000000076_d_al41zguyvgqfj2454jki5l55_20071203[1]' 

Where the capture groups return something like:

$1  =  header_0000000602_ $2  =  _mc2e1nrobr1a3s55niyrrqvy_20081212 $3  =  .doc 

Where $3 can be empty if no file extension is found. $3 is the optional part, as you can see in str3 above.

If I add ‘?’ to the end of the third capture group ‘(.\w{3,4})?’, the RegEx no longer captures $3 for any string. If I add ‘+’ instead ‘(.\w{3,4})+’, the RegEx no longer captures str3 at all, which is to be expected.

I feel that using ‘?’ at the end of the third capture group is the appropriate thing to do, but it doesn’t work as I expect. I am probably being too naive with the ‘.*’ sections that I use to ignore parts of the string.

Doesn’t Work As Expected:

.*(header_\d*_).*(_.*_.{8}).*(\.\w{3,4})?.* 
  • 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. 2026-05-11T05:14:49+00:00Added an answer on May 11, 2026 at 5:14 am

    One possibility is that the second to last .* is being greedy. You might try changing it to:

    .*(header_\d*_).*(_.*_.{8}).*?(\.\w{3,4})?.*                              ^ Added that 

    That wasn’t correct, this one will match the input you supplied, but it assumes that the first . it encounters is the start of a file extension:

    .*(header_\d*_).*(_.*_.{8})[^\.]*(\.\w{3,4})?.* 

    Edit: Remove the escaping I had in the second regex.

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

Sidebar

Related Questions

How can I fix this? REGEX: //REGEX $match_expression = '/Rt..tt<\/td> <td>(.*)<\/td>/'; preg_match($match_expression,$text,$matches1); $final =
Can someone explain to me how I can fix this problem. I have two
I can't fix this problem on my listview template: i have the error as
How can I fix this error, and install thin or other gems? $ sudo
The Term::Size-module jumbles up the encoding. How can I fix this? #!/usr/bin/env perl use
My navbar doesn't line up in IE... how can I fix this without using
Can anyone explain the output of this program and how I can fix it?
I can't seem to find a way to fix this problem. All I'm doing
I'm getting this error in the log.... What can I do to fix this?
How can I fix the view of this twitter on my blogger blog. Here

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.