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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:55:41+00:00 2026-06-09T23:55:41+00:00

Here is my situation: re.sub(r'([^\\])’, r’\1[\W\1]*’, string) It is straight forward that I want

  • 0

Here is my situation:

re.sub(r'([^\\])', r'\1[\W\1]*', string)

It is straight forward that I want to append [\W(itself)] after (itself) for itself being a group of characters (can be special). That is why I need to put it in a set to strip away all special meanings. However, my group can be a SET. I know that nested sets do not work. How do I escape / remove the square brackets to safely put my group in the set?

My other attempt was to use \1(\W|\1)* instead, but I need to escape characters in my group without escaping possible square brackets in the group. How do I do so?

This is a dilemma. I do not know how to solve this problem and which way to go. Please help.

Thank you very much.

EDIT:
I skipped a step. After matching a character but \ (the [^\] part) and replace with the explained expression, I will sometime need to replace it with a set of similar characters. So, ‘a’ becomes ‘[a@]’, ‘s’ becomes ‘[s5$]’, etc…
The question was really wrong. But I solved the problem, so if you are still trying to make some sense out of what I wrote earlier, please don’t 🙂

  • 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-09T23:55:43+00:00Added an answer on June 9, 2026 at 11:55 pm

    You can use a function as the replacement in re.sub. This will allow you to call re.escape on your match before performing the substitution:

    def escape_repl(match):
        return '{0}[\W{0}]*'.format(re.escape(match.group(1)))
    
    re.sub(r'([^\\])', escape_repl, string)
    

    Example:

    >>> print re.sub(r'([^\\])', escape_repl, '[^$]')
    \[[\W\[]*\^[\W\^]*\$[\W\$]*\][\W\]]*
    

    I think this is what you are trying to do, but it is a little unclear from your question. Please provide some sample strings and expected results if this isn’t what you’re looking for.

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

Sidebar

Related Questions

I have an situation here that looks very like a SELECT N+1 from the
Here is my situation, I have a string as follows 'a':1 'b':2 'c':3 I
Here is my situation. I need to create a report that shows each open
Here's the situation: I have a custom TextBox control that contains multiple other TextBox
Sorry for the long title. Here is a sample situation that I encounter often
I have a group of functions that use Sub-Range types for their input parameter.
I have a specific situation where I've got an object that I want to
Here is the situation. I have 3 tables, one super type, and two sub
Here the Situation . I am having 2 million records in table . Just
I have a strange situation here. In my android app, I have a layout

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.