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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:24:55+00:00 2026-06-03T03:24:55+00:00

I am trying to add a more efficient swear filter to a chat service

  • 0

I am trying to add a more efficient swear filter to a chat service we have and can’t seem to get my regular expression working on my live django server.

I am running the latest stable Django on Python 2.6.7.

Here is my code:

def replacement(match):
    return "*" * len(match.group(0))

def censored_string(cFilter, dirty):
    clean = str(dirty)
    wordList = cFilter.mutations.split(',')
    wordList.sort(key = len)
    wordList.reverse()

    for swear_word in wordList:
        target_word = swear_word.strip()
        result = re.sub("(?i)\\b(("+target_word+"){1,})(s{0,1})\\b",replacement, clean)
        clean = result
    return clean

for the record – this works using my local server setup which I can confirm is also using python 2.6.7 and the same django version, however I have not done much django or python since about 10 months ago and inherited this server setup recently – if there is something I am missing please let me know.

the output of the error is as follows:

{
    "error_message": "multiple repeat",
    "traceback": ... "result = re.sub(\"(?i)\\\\b(\"+target_word+\"){1,}(s{0,1})\\\\b\",censored_word(target_word), clean)\n\n  File \"/usr/lib/python2.6/re.py\", line 151, in sub\n    return _compile(pattern, 0).sub(repl, string, count)\n\n  File \"/usr/lib/python2.6/re.py\", line 245, in _compile\n    raise error, v # invalid expression\n\nerror: multiple repeat\n"
}

I have tried with and without greedy’s and so forth but am lost now – any input would be greatly appreciated

cheers,

Michael

  • 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-03T03:24:57+00:00Added an answer on June 3, 2026 at 3:24 am

    I don’t think the problem is with the regex, but with your word list. It’s likely the list contains characters which are interpreted as regex special characters. This works for me:

    #!/usr/bin/python
    import re
    
    def replacement(match):
        return "*" * len(match.group(0))
    
    def censored_string(dirty):
        clean = str(dirty)
        wordList = ["fuck", "shit", "damn"]
    
        for swear_word in wordList:
            target_word = swear_word.strip()
            result = re.sub("(?i)\\b(("+target_word+"){1,})(s{0,1})\\b",replacement, clean)
            clean = result
        return clean
    
    print censored_string("god damn i love bananas and fucking fuckfuck shits")
    # god **** i love bananas and fucking ******** *****
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to add an onclick handler to my tabs, and can't seem to get
I'm trying to work out a more efficient way to add a note count,
I'm working on a Rails application where I have users that can add each
I'm trying to use fixtures to add more complex test data in order to
Trying to add tests to the gobject-introspection test suite to cover more of the
Trying to add a new element to the DOM but I get all sorts
I have a classifieds system I'm working on. People are able to add photos
I'm trying to think of the most efficient way to add html to my
Ok so I'm basically trying to determine which way is more efficient performance wise
I'm working on a simple AJAX chat add-on for my PHP application so I

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.