Python Forum

Full Version: [split] [split] regular expression
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Could you help me out a bit? I'd like to remove those colored characters out of a bunch of files like these:

a = 'Python Tutorial for Beginners 1 - Install and Setup for Mac and Windows-YYXdXT2l-Gg.mkv'
b = 'Python Tutorial for Beginners 2 - Strings - Working with Textual Data-k9TUPpGqYTo.mkv'

I'm here so far:

def replacer(filename):
    n = re.findall('-[^a-zA-Z0-9]', filename)
    print(n)
The regex would be like this

always starts with a - character, followed by a letter, until a . which is the file extension.

Thanks
(Jun-24-2018, 08:24 AM)kerzol81 Wrote: [ -> ]Hi,

Could you help me out a bit? I'd like to remove those colored characters out of a bunch of files like these:

a = 'Python Tutorial for Beginners 1 - Install and Setup for Mac and Windows-YYXdXT2l-Gg.mkv'
b = 'Python Tutorial for Beginners 2 - Strings - Working with Textual Data-k9TUPpGqYTo.mkv'

I'm here so far:

def replacer(filename):
    n = re.findall('-[^a-zA-Z0-9]', filename)
    print(n)
The regex would be like this

always starts with a - character, followed by a letter, until a . which is the file extension.

Thanks

That will do the trick
re.sub(r'-\w+(?=\.))', '', 'Windows-YYXdXT2l-Gg.mkv')
but if you what to use RegEx - you better learn them. This site is the excellent playing ground to learn RE.