Home > OS >  RegEx: Remove all content until first third special character
RegEx: Remove all content until first third special character

Time:01-21

I‘m trying to remove all content from a string until the first third slash using Python.

This/is/a/simple/test

Should be

simple/test

CodePudding user response:

You can use the re.sub() function to replace the parts of the string before the third slash with an empty string.

import re
string = "This/is/a/simple/test"
new_string = re.sub(r'^(?:[^/]*/){3}', '', string)
print(new_string)

CodePudding user response:

Why use ReGex, when you could just split the string, index it, then join it together.

string = "This/is/a/simple/test"
string_list = string.split("/")[3:]
new_string = "/".join(string_list)
print(new_string)
  • Related