12

I'm trying to recreate the strip() function of python using Regex. It's the last practice problem from Automate the Boring Stuff with Python. Here's my code:

import re

stripChar = input('Enter character to strip: ')
context = input('Enter string to strip: ')
stripContext = None


def strip(char, string):
    if stripChar == "":
        regsp = re.compile(r'^\s+|\s+$')
        stripContext = regsp.sub("", context)
        return stripContext
    else:
        stripContext = re.sub(r'^(char)+', "", string)
        return stripContext

print(strip(stripChar, context))

In line 16, if I replace (char) with any random character, the program is working. However, I can't seem to make a custom variable work there. What am I doing wrong there?

Edit: Stack is saying it's a duplicate of this question. It's not because it' s purely around Regex not only Python.

ahmed_imtiaz
  • 143
  • 1
  • 1
  • 7
  • 1
    Can you add any sample input and o/p ? – Vikas Periyadath Apr 27 '18 at 05:10
  • Possible duplicate of https://stackoverflow.com/questions/5082452/python-string-formatting-vs-format – Taku Apr 27 '18 at 05:12
  • 2
    You are not referring to the variable. `"char"` is a literal string of four characters, not the value of the namesake variable. Consider learning how to use `.format()`. As a side note, there is no point in compiling a revel if you do not use it more than once. – DYZ Apr 27 '18 at 05:12

7 Answers7

5

I slightly changed your script like this,

def strip(char, string):
    if char == "":                # not "stripChar"
        regsp = re.compile(r'^\s+|\s+$')
        stripContext = regsp.sub("", string)
        return stripContext
    else:                       # some changes are here in this else statement
        stripContext = re.sub(r'^{}+|{}+$'.format(char,char), "", strip("",string))
        return stripContext

print(strip(stripChar, context))

Output:

Enter character to strip: e
Enter string to strip:   efdsafdsaeeeeeeeeee
fdsafdsa
fzzylogic
  • 2,183
  • 1
  • 19
  • 25
Thm Lee
  • 1,236
  • 1
  • 9
  • 12
3

I did it that simple way and it worked for me.

import re

def my_strip(string, char=''):
    regex_sub = re.sub(r'^\s+|\s+$', char, string)
    return(regex_sub)
2

You could do it like this using re.sub

import re

def strip(string, chars=' \n\r\t'):
    return re.sub(r'(?:^[{chars}]+)|(?:[{chars}]+$)'.format(chars=re.escape(chars)), '', string)

It uses re.escape, so users can enter characters like \ and [ that have meaning withing regex strings. It also uses the ^ and $ regex tokens so that only groups of matching characters at the front and end of the string are matched.

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
1

Here's a simplified version.

import re
def striper(x, y=""):
    if y == "":
        rex = re.compile(r'^(\s*)|(\s)*$')
        xy = rex.sub("", x)
        return xy
    else:
        stripContext = re.sub(r'^{}+|{}+|{}+$'.format(y, y, y), "", x)
        return stripContext
print(striper('abcdfsdfdsabc', 'abc'))
Rajinish
  • 11
  • 1
1

You can do it with one compile using an optional variable. No raw statement needed since there are no escape characters.

import re

def regexStrip(theString, stripChar='\s'):
    theRegex = re.compile(f'^({stripChar})*|({stripChar})*$')
    stripContext = theRegex.sub('', theString)
    return stripContext

print(regexStrip('SpamEggsSpam','Spam'))
print(regexStrip('SpamSpamSpam$Eggs$SpamSpamSpam','Spam'))
print(regexStrip('    Eggs    '))
print(regexStrip('   $ Eggs $   '))
0

To have lstrip and rstrip, simply adapt Brendan's answer to the following:

import regex as re

def lregstrip(string, chars=' \n\r\t\f\v'):
    return re.sub(r'(?:^[{chars}]+)'.format(chars=re.escape(chars)), '', string)

def rregstrip(string, chars=' \n\r\t\f\v'):
    return re.sub(r'(?:[{chars}]+$)'.format(chars=re.escape(chars)), '', string)

def regstrip(string, chars=' \n\r\t\f\v'):
    return rregstrip(lregstrip(string,chars),chars)

candidate = "  \t hogo  hohohoh oho hohoho h         \n \f"
print("-"+regstrip(candidate)+"-")
0

I did it in that way:

import re

def new_strip(string, argument = r'\s'):   
    strip_regex = re.sub(re.compile(rf'^{argument}+|{argument}+$'), "", string)
    return strip_regex

print(new_strip('    4564dsf4asdfa    r68a    .\n\n\n'))
print(new_strip('ffffffffffffffdsafhiuhfdsffffffffffffffff', 'f'))

Output:

4564dsf4asdfa    r68a    .
dsafhiuhfds

It's simple and works well because it is not necessary to check whether the argument is a space character or not, it is assumed that it is unless otherwise stated.