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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:26:26+00:00 2026-05-14T15:26:26+00:00

I need to match two cases by one reg expression and do replacement ‘long.file.name.jpg’

  • 0

I need to match two cases by one reg expression and do replacement

‘long.file.name.jpg’ -> ‘long.file.name_suff.jpg’

‘long.file.name_a.jpg’ -> ‘long.file.name_suff.jpg’

I’m trying to do the following

re.sub('(\_a)?\.[^\.]*$' , '_suff.',"long.file.name.jpg")

But this is cut the extension ‘.jpg’ and I’m getting

long.file.name_suff. instead of long.file.name_suff.jpg
I understand that this is because of [^.]*$ part, but I can’t exclude it, because
I have to find last occurance of ‘_a’ to replace or last ‘.’

Is there a way to replace only part of the match?

  • 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-05-14T15:26:26+00:00Added an answer on May 14, 2026 at 3:26 pm
     re.sub(r'(?:_a)?\.([^.]*)$', r'_suff.\1', "long.file.name.jpg")
    

    ?: starts a non matching group (SO answer), so (?:_a) is matching the _a but not enumerating it, the following question mark makes it optional.

    So in English, this says, match the ending .<anything> that follows (or doesn’t) the pattern _a

    Another way to do this would be to use a lookbehind (see here). Mentioning this because they’re super useful, but I didn’t know of them for 15 years of doing REs

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

Sidebar

Related Questions

I need a regular expression that will match the file name of a ResourceBundle
Here are my two reg expressions. The first one seems to match my case
I need to create a regular expression to match strings that satisfy any two
I need to match a image url like this: http://site.com/site.com/files/images/img (5).jpg Something like this
I need to match a fixed width field on a file layout with a
hi I need to read a text file and then explode it to two
I need to compare two columns in a sql table. The data in one
I've got a layout with two editexts. I need to set the second one
I need to compare strings and match names to one another even if they
I need to use NSPredicate to match two strings, case-insensitive, diacritic insensitive, and whitespace-insensitive

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.