Remove regex from string python

I'm a beginner with both Python and RegEx, and I would like to know how to make a string that takes symbols and replaces them with spaces. Any help is great.

For example:

how much for the maple syrup? $20.99? That's ricidulous!!!

into:

how much for the maple syrup 20 99 That s ridiculous

asked May 18, 2009 at 1:55

2

One way, using regular expressions:

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
  • \w will match alphanumeric characters and underscores

  • [^\w] will match anything that's not alphanumeric or underscore

Remove regex from string python

Blue Ice

7,7086 gold badges31 silver badges50 bronze badges

answered May 18, 2009 at 1:59

dF.dF.

72k29 gold badges128 silver badges135 bronze badges

5

Sometimes it takes longer to figure out the regex than to just write it out in python:

import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')

If you need other characters you can change it to use a white-list or extend your black-list.

Sample white-list:

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '

Sample white-list using a generator-expression:

whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)

answered May 18, 2009 at 9:24

Remove regex from string python

monkutmonkut

40.1k22 gold badges117 silver badges147 bronze badges

2

I often just open the console and look for the solution in the objects methods. Quite often it's already there:

>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'

Short answer: Use string.replace().

Nathan

3,7292 gold badges19 silver badges21 bronze badges

answered May 18, 2009 at 5:45

busterbuster

9511 gold badge8 silver badges15 bronze badges

1

Not the answer you're looking for? Browse other questions tagged python regex string or ask your own question.

How do I remove a character from a string in regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

How do I remove a specific pattern from a string in Python?

In Python you can use the replace() and translate() methods to specify which characters you want to remove from the string and return a new modified string result. It is important to remember that the original string will not be altered because strings are immutable. Here is the basic syntax for the replace() method.

How do you remove special characters from a string in Python regex?

Using 're..
“[^A-Za-z0–9]” → It'll match all of the characters except the alphabets and the numbers. ... .
All of the characters matched will be replaced with an empty string..
All of the characters except the alphabets and numbers are removed..

How do I delete a regular expression?

How to remove text in brackets and parentheses using regex.
On the Ablebits Data tab, in the Text group, click Regex Tools..
On the Regex Tools pane, select your source strings, enter your regex, choose the Remove option, and hit Remove. To get the results as formulas, not values, select the Insert as a formula check box..