ahmed_imtiaz
ahmed_imtiaz

Reputation: 143

Strip() Function Using Regex

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.

Upvotes: 12

Views: 15982

Answers (7)

Moisés Augusto
Moisés Augusto

Reputation: 1

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.

Upvotes: 0

Thm Lee
Thm Lee

Reputation: 1236

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

Upvotes: 5

Giovani Valente
Giovani Valente

Reputation: 31

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)

Upvotes: 3

LampshadeTricky
LampshadeTricky

Reputation: 11

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 $   '))

Upvotes: 1

Rajinish
Rajinish

Reputation: 11

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'))

Upvotes: 1

Jordy Van Landeghem
Jordy Van Landeghem

Reputation: 17

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)+"-")

Upvotes: 0

Brendan Abel
Brendan Abel

Reputation: 37539

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.

Upvotes: 2

Related Questions