Python Forum
[split] [split] regular expression
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] [split] regular expression
#1
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
Reply
#2
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] University Assignment Help needed Nomathemba 4 3,572 Apr-20-2024, 12:24 PM
Last Post: jas31
  [split] How to ask Smart Questions (thread title expansion) saifalikhan9 0 310 Feb-17-2024, 11:57 AM
Last Post: saifalikhan9
  [split] new to python hello world code gives errors giobastidas1970 1 1,456 Mar-11-2023, 12:48 PM
Last Post: jefsummers
  [split] assignment help Shobana 2 1,313 Nov-11-2022, 12:10 PM
Last Post: Larz60+
  name.split() + (And) + print question sklord 12 3,716 Mar-26-2022, 05:45 PM
Last Post: deanhystad
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,608 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  [split] How to calculate tax with a variable input and if else rokemas 1 2,125 Dec-28-2020, 06:53 AM
Last Post: buran
Sad [split] HELP ME DEBUG - Who wants to be a millionaire Game Beetoh 1 2,056 Dec-18-2020, 06:09 PM
Last Post: buran
  [split] Python for Everybody 5.2 assignment ramadan2099 3 12,070 Jul-15-2020, 04:54 PM
Last Post: Bipasha
  [split] help me make this code better please (basic) Rustam 2 2,264 Jun-19-2020, 01:27 PM
Last Post: Rustam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020