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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:08:43+00:00 2026-06-04T13:08:43+00:00

I am looking for a regex for some filename parsing in order to count

  • 0

I am looking for a regex for some filename parsing in order to count the number of instances that a filename prefix occurs. Here are some sample strings

gloves.tga
10jeans.jpg
shirt1.png
shirt2.png
coat_00.png
coat_12.gif
top1_01.png
top2_04.png

The basic pattern is just a string of letters or numbers followed by an extension. The prefix is everything before the extension (excluding the period)

A single piece of clothing may be spread across multiple files, indicated by the clothing name, followed by an underscore, followed by some index numbers and then the extension. The prefix is everything up to but not including the underscore. Everything else can be ignored.

This covers all of the cases I’m working with, but I’m having trouble working with the fact that one case has an underscore while the other case doesn’t.

Can someone help me come up with a regex for this?

EDIT: There seems to be an extra condition: shirt1 and shirt2 should be treated as the same prefix.

So if a string is followed by some numbers, and immediately followed by an extension, then the numbers should be ignored, whereas if the numbers were followed by an underscore, then they would be kept in the prefix.

  • 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-04T13:08:44+00:00Added an answer on June 4, 2026 at 1:08 pm

    Won’t this work? (Perl/PCRE syntax)

    /^([^._]+)/ 
    

    That will capture the longest prefix of the string that contains no periods or underscores.

    EDIT: OK, if shirt is the prefix in shirt1, then you can try something like this:

    /^([^._]+)(?<!\d)/
    

    which disallows prefixes that end in a digit. That won’t work in Ruby 1.8, though, since 1.8 doesn’t have lookbehind assertions.

    EDIT 2:
    The above means that the prefix of top1_01 is top, but we want that one to include the digits before the underscore. So our last attempt is to add an alternative:

    /^([^._]+)(?:(?<!\d)|(?=_))/
    

    The prefix has to either not end in a digit or be followed by an underscore.
    Demo:

    %w<gloves.tga  10jeans.jpg shirt1.png  shirt2.png 
       coat_00.png coat_12.gif top1_01.png top2_04.png>.each do |filename|
      if m = filename.match(/^([^._]+)(?:(?<!\d)|(?=_))/) then
        puts [ filename, m[1] ].join ":\t"
      else
        warn "Uh-oh, couldn't find a prefix in filename '#{filename}'."
      end
    end    
    

    Output:

     gloves.tga:    gloves
     10jeans.jpg:   10jeans
     shirt1.png:    shirt
     shirt2.png:    shirt
     coat_00.png:   coat
     coat_12.gif:   coat
     top1_01.png:   top1
     top2_04.png:   top2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help. I am looking for a regex that would match the
Looking for some Regex help in Ruby. I have a string that is formated
I'm looking for a regex that matches strings with a given length (parameterized) that
I'm looking for some help from a regex guru, here's what I want to
I'm looking for some regex for this kind of strings rgb(r,g,b) rgba(r,g,b,a) hsl(h,s%,l%) hsla(h,s%,l%,a)
Regex-noob here. Looking for some C# regex code to syntax highlight twitter text. So
I'm looking for a regex that will recognize # followed by a number in
I'm looking for some regex/automata help. I'm limited to + or the Kleene Star.
Regexs make me cry, so, I came here for help. I'm looking for some
I'm looking a symbol regex in PHP that will check submitted usernames and reject

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.