0

I have a string that will have digits in that, we need to remove all the characters which are not digits and replace the digits with #

I have written a regex, its able to replace the digits with #, but I could not find the regex to remove the characters which are not digits.

import re
def replace_digits(string):
    m=re.sub("\d","#",string)

Examples :

234 -> ###

a2b3c4 -> ###

abc -> <empty string>

#2a$#b%c%561# -> ####

Corentin Limier
  • 4,946
  • 1
  • 13
  • 24
prashanth
  • 65
  • 1
  • 4
  • Ex 1: A = 234 Output: ### Ex 2: A = a2b3c4 Output: ### Ex 3: A = abc Output: (empty string) Ex 5: A = #2a$#b%c%561# Output: #### – prashanth Jul 12 '19 at 12:56
  • 2
    Is there a specific reason you want use regex? looks like can be done easily without: `'#' * len(x for x in s if x.isdigit())` – Tomerikoo Jul 12 '19 at 13:01

1 Answers1

2
import re

examples = ['234',
'a2b3c4',
'abc',
'#2a$#b%c%561#']

for example in examples:
    new_s = '#' * len(re.sub(r'\D', '', example))
    print('Input = {} Output = {}'.format(example, new_s))

Prints:

Input = 234 Output = ###
Input = a2b3c4 Output = ###
Input = abc Output = 
Input = #2a$#b%c%561# Output = ####

EDIT (without regex - thanks @CorentinLimier)

for example in examples:
    new_s = ''.join('#' for c in example if c.isdigit())
    print('Input = {} Output = {}'.format(example, new_s))

EDIT (add @Tomerikoo 's answer from the comments):

for example in examples:
    new_s = '#' * len([x for x in example if x.isdigit()])
    print('Input = {} Output = {}'.format(example, new_s))
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    No need to use a meta sequence inside a group. instead of `[^\d]` simply use `\D` – Tomerikoo Jul 12 '19 at 13:08
  • 1
    Imo there is a more pythonic way to code your idea `''.join('#' if c.isdigit() else '' for c in s)` it is more readable – Corentin Limier Jul 12 '19 at 13:09
  • 1
    @AndrejKesely Tomerikoos non regex solution in the comments above is probablyt slightly more readable (and less confusing for beginners since it doesn't use `((`) then the non regex solution suggested by Corentin. – Error - Syntactical Remorse Jul 12 '19 at 13:18
  • 2
    @Error-SyntacticalRemorse Added it to the response. But I edited it slightly - you cannot do `len()` on plain generator expression, so I enclosed it in `[]` – Andrej Kesely Jul 12 '19 at 13:33
  • 2
    @AndrejKesely Thanks for that. I wasn't sure if it is necessary or not as I know that for printing for example it can be ommited. As to @CorentinLimier 's version, I believe it can be simplified by removing the else part and moving the if to the end: `''.join('#' for c in example if c.isdigit())` – Tomerikoo Jul 12 '19 at 13:43
  • 1
    @Tomerikoo Yes, you have right. I edited it. Now op has rich options of answers to choose from ;) – Andrej Kesely Jul 12 '19 at 13:48
  • I have been searching the internet on regex patterns to get good understanding, but could not find an easy one. Could anyone suggest some link where I can get sufficient practical knowledge on regex patterns in python. – prashanth Jul 13 '19 at 09:56