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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:56:42+00:00 2026-05-11T06:56:42+00:00

In Python, is there a better way to parameterise strings into regular expressions than

  • 0

In Python, is there a better way to parameterise strings into regular expressions than doing it manually like this:

test = 'flobalob' names = ['a', 'b', 'c'] for name in names:     regexp = '%s' % (name)     print regexp, re.search(regexp, test) 

This noddy example tries to match each name in turn. I know there’s better ways of doing that, but its a simple example purely to illustrate the point.


The answer appears to be no, there’s no real alternative. The best way to paramaterise regular expressions in python is as above or with derivatives such as str.format(). I tried to write a generic question, rather than ‘fix ma codez, kthxbye’. For those still interested, I’ve fleshed out an example closer to my needs here:

for diskfilename in os.listdir(''):     filenames = ['bob.txt', 'fred.txt', 'paul.txt']     for filename in filenames:         name, ext = filename.split('.')         regexp = '%s.*\.%s' % (name, ext)         m = re.search(regexp, diskfilename)         if m:           print diskfilename, regexp, re.search(regexp, diskfilename)           # ... 

I’m trying to figure out the ‘type’ of a file based on its filename, of the form <filename>_<date>.<extension>. In my real code, the filenames array is a dict, containing a function to call once a match is found.

Other ways I’ve considered doing it:

  • Have a regular expression in the array. I already have an array of filenames without any regular expression magic, so I am loathe to do this. I have done this elsewhere in my code and its a mess (though necessary there).

  • Match only on the start of the filename. This would work, but would break with .bak copies of files, etc. At some point I’ll probably want to extract the date from the filename so would need to use a regular expression anyway.


Thanks for the responses suggesting alternatives to regular expressions to achieve the same end result. I was more interested in parameterising regular expressions for now and for the future. I never come across fnmatch, so its all useful in the long run.

  • 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-11T06:56:43+00:00Added an answer on May 11, 2026 at 6:56 am

    Well, as you build a regexp from a string, I see no other way. But you could parameterise the string itself with a dictionary:

    d = {'bar': 'a', 'foo': 'b'} regexp = '%(foo)s|%(bar)s' % d 

    Or, depending on the problem, you could use list comprehensions:

    vlist = ['a', 'b', 'c'] regexp = '|'.join([s for s in vlist]) 

    EDIT: Mat clarified his question, this makes things different and the above mentioned is totally irrelevant.

    I’d probably go with an approach like this:

    filename = 'bob_20090216.txt'  regexps = {'bob': 'bob_[0-9]+.txt',            'fred': 'fred_[0-9]+.txt',            'paul': 'paul_[0-9]+.txt'}  for filetype, regexp in regexps.items():     m = re.match(regexp, filename)     if m != None:         print '%s is of type %s' % (filename, filetype) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 92k
  • Answers 92k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How are you querying the data? I've found that the… May 11, 2026 at 6:23 pm
  • Editorial Team
    Editorial Team added an answer I used an entry like this in my samba configuration… May 11, 2026 at 6:23 pm
  • Editorial Team
    Editorial Team added an answer There are two special array @- and @+ containing the… May 11, 2026 at 6:23 pm

Related Questions

In Python on Windows, is there a way to determine if a user has
I am writing a simple Python web application that consists of several pages of
I like that in PHP I can do the following $myInteger++; $myString += 'more
What is the best way to choose a random file from a directory in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.