0

I am fairly new to Python, and I am learning about Regexes right now, which has been a bit of a challenge for me. My issue right now is I am working on a problem that is to create a function that is a Regex version of the strip() string method.

My problem is that I can't figure out how to convert a character that user inputs into a regex without listing out every possibility in the program with if statements. For instance:

def regexStrip(string, char):
    if char = 'a' or 'b' or 'c' etc...
        charRegex = re.compile(r'^[a-z]+')

This isn't my full program just a few lines to demonstrate what I'm talking about. I was wondering if anyone could help me in finding a more efficient way to convert user input into a Regex.

ragnar723
  • 1
  • 1
  • Can you please clarify what will be the user input in your example? Does the user provide a regex? If not what is the relation between user input and the resultant regex? – Krishna Sep 11 '18 at 16:17
  • Hi! Welcome to StackOverflow! I am not sure I understand what your question exactly is. Anyway, regular expressions were invented precisely for writing complex conditions on strings in a concise way. While there are better ways to write the second line of your code, e.g. `if char in string.ascii_lowercase:` (provided you have issued `import string` earlier), this is, in general, not simple. Besides, that regex would not be matching the same as the `if` condition. – norok2 Sep 11 '18 at 16:20
  • Check this [answer](https://stackoverflow.com/questions/50055600/strip-function-using-regex) to see if it answers your question – tzee Sep 11 '18 at 16:24
  • Yeah sorry, I apologize for being unclear, I am new to this. I am supposed to be creating a function that takes two arguments. The first one is a string, and the second one is a character that is to be stripped from the string. The function is supposed to do what the strip() string method does. – ragnar723 Sep 11 '18 at 16:26

1 Answers1

1

You can use braces inside strings and the format function to build the regular expression.

def regexStrip(string, char=' '):
    #Removes the characters at the beginning of the string
    striped_left = re.sub('^{}*'.format(char), '', string)
    #Removes the characters at the end of the string
    striped = re.sub('{}*$'.format(char), '', striped_left)
    return striped

The strip method in python allows to use multiples chars, for example you can do 'hello world'.strip('held') and it will return 'o wor'

To perform this, you can do :

def regexStrip(string, chars=' '):
    rgx_chars = '|'.join(chars)
    #Removes the characters at the beginning of the string
    striped_left = re.sub('^[{}]*'.format(rgx_chars), '', string)
    #Removes the characters at the end of the string
    striped = re.sub('[{}]*$'.format(rgx_chars), '', striped_left)
    return striped

If you want to use search matching instead of substitutions, you can do :

def regexStrip(string, chars=' '):
    rgx_chars = '|'.join(chars)
    striped_search = re.search('[^{0}].*[^{0}]'.format(rgx_chars), string)
    if striped_search :
        return striped_search.group()
    else:
        return ''
Corentin Limier
  • 4,946
  • 1
  • 13
  • 24