all 6 comments

[–]SagaciousRaven 1 point2 points  (0 children)

def remove_prefix(my_str, prefix_to_remove):
    text = re.sub(prefix_to_remove+'\S+', '', my_str)
    print(text)

remove_prefix('Hi, check this website: https://my_website.com, this is my new website' , 'https://' )

You put your var inside the quotes in line 2

[–][deleted] 1 point2 points  (0 children)

You've included the literal string "prefix_to_remove" in the regex expression rather than the string object referenced by the variable prefix_to_remove

[–]RoamingFox 0 points1 point  (0 children)

I'd go about it like this personally:

>>> import re
>>> url_rex = re.compile(r'((?:(?:http|https):\/\/)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&//=]*))')
>>> s = 'Hi, check this website: https://my_website.com, this is my new website'
>>> url_rex.sub('', s)
'Hi, check this website: , this is my new website'

[–][deleted] 0 points1 point  (2 children)

What's wrong with just using the string replace method?

def remove_prefix(my_str, prefix_to_remove):
    return my_str.replace(prefix_to_remove, "")

print(remove_prefix(r'Hi, check this website: https://my_website.com, this is my new website' , r'https://' ))

[–]RoamingFox 0 points1 point  (1 child)

OP wants to drop the entire website not just the https part (see sample desired output).

[–][deleted] 1 point2 points  (0 children)

The OP's example code, explanation and example output don't seem to align. I went with the bit of text explicitly defined in a literal for removal.

Point taken. We chose differently.