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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:33:37+00:00 2026-06-12T13:33:37+00:00

Of note: maximum string ling will be 15 char.

  • 0

Of note: maximum string ling will be 15 char.

  • 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-12T13:33:38+00:00Added an answer on June 12, 2026 at 1:33 pm

    To compare raw speed here are here’s a comparison of @wim’s answer, a few optimizations of it and a regex solution:

    import re, string, timeit
    
    clean = re.compile(r'[^a-zA-Z0-9_-]')
    keep = string.ascii_letters + string.digits + '_-'
    keep_set = set(string.ascii_letters + string.digits + '_-')
    test = '$pam and_Eggs##-!'
    cur_encoding_bytes = 256 # 8 for UTF-8, in Python 3 this would be different for Unicode
    all_else = ''.join(chr(i) for i in range(cur_encoding_bytes) if chr(i) not in keep_set) # taken from https://stackoverflow.com/a/3588485/1219006
    
    def clean1(s):
        return ''.join(x for x in s if x in keep)
    
    def clean1_filter(s):
        return filter(keep.__contains__, s)
    
    def clean1_filter_set(s):
        return filter(keep_set.__contains__, s)
    
    def clean2(s):
        return clean.sub('', s)
    
    def clean3(s):
        return s.translate(None, all_else)
    
    print timeit.timeit('clean1(test)', 'from __main__ import clean1, test')
    print timeit.timeit('clean1_filter(test)', 'from __main__ import clean1_filter, test')
    print timeit.timeit('clean1_filter_set(test)', 'from __main__ import clean1_filter_set, test')
    print timeit.timeit('clean2(test)', 'from __main__ import clean2, test')
    print timeit.timeit('clean3(test)', 'from __main__ import clean3, test')
    

    The output of this script is:

    2.96962522809
    1.56208783165
    1.10597814849
    1.59298783663
    0.53834820236
    

    str.translate is the fastest, being a specialized string method.

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

Sidebar

Related Questions

I want to limit the maximum memory used by the JVM. Note, this is
I would like to know what is the maximum length of a String when
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code
Note: I am not asking how to use Google Code's SVN repo as a
note: i only want to move 1 shape at a time Circle.prototype.create = function(){
Note: Not sure if this is the right stack, please tell if I should
NOTE: This is a followup to my question here. I have a program that
NOTE: This is an old question and the answers here no longer works (since
Note: I had another similar question about how to GZIP data using Ruby's zlib

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.