Help creating a pattern using the re module - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help creating a pattern using the re module (/thread-15231.html) |
Help creating a pattern using the re module - zulu_likuum - Jan-08-2019 I have written a python3 script to rename files based on a pattern input by the user. It has worked well, but I now am trying to create a pattern, and I am running into problems. I have four types of filenames: filename 1 - flubber.MP4 filename 2.MP4 filename 3 - flubber.mp4 filename 4.mp4 I am trying to create a pattern that will work on all these file names and rename them all as: filename1.mp4 filename2.mp4 filename3.mp4 filename4.mp4 (This one of course would not be renamed) I can easily accomplish this in two passes, but I would like to accomplish it in one pass. Help? RE: Help creating a pattern using the re module - Gribouillis - Jan-09-2019 How do you easily accomplish this in two passes? RE: Help creating a pattern using the re module - micseydel - Jan-09-2019 What's your code for the two-pass solution? You mention that the 4th should not be renamed, but you actually remove a space. It might be good to clarify which it is. RE: Help creating a pattern using the re module - zulu_likuum - Jan-09-2019 (Jan-09-2019, 12:16 AM)micseydel Wrote: What's your code for the two-pass solution? Wow. I knew something was different. I couldn't find a way to edit my post, so allow me to correct it here. I have written a python3 script to rename files based on a pattern input by the user. It has worked well, but I now am trying to create a pattern, and I am running into problems. Correction: I have four types of filenames: filename1 - flubber.MP4 filename2.MP4 filename3 - flubber.mp4 filename4.mp4 I am trying to create a pattern that will work on all these file names and rename them all as: filename1.mp4 filename2.mp4 filename3.mp4 filename4.mp4 (This one of course would not be renamed) I can easily accomplish this in two passes, but I would like to accomplish it in one pass. Help? (Jan-09-2019, 12:16 AM)Gribouillis Wrote: How do you easily accomplish this in two passes? See my correction in response to micseydel. My two-pass solution is: 1st pattern: (.+)MP4$ Replacement: \1mp4 2nd pattern: (.+) - flubber(.+) Replacement: \1\2 RE: Help creating a pattern using the re module - Gribouillis - Jan-09-2019 You could replace (?: - flubber)?\.(?:mp|MP)4$ with .mp4
RE: Help creating a pattern using the re module - zulu_likuum - Jan-09-2019 (Jan-09-2019, 07:38 AM)Gribouillis Wrote: You could replace That worked great. Thanks a lot. I think it's making sense. But let me make sure I understand what I think I understand. In this case when I compile a pattern and use patt.sub(repl, filename) , I didn't need to capture the beginning of the string with (.+) because the entire string is already going to be returned with only the changes applied?Thanks again. RE: Help creating a pattern using the re module - Gribouillis - Jan-09-2019 Quote:the entire string is already going to be returned with only the changes applied?Exactly |