Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a regex? (solved)
#1
I'm trying to isolate the number in a filename that can look like these:

DJI_0991.MP4
DJI_0992 - testing.MP4

The pattern is that it always starts with DJI_ and ends with either a period or a space.

Nevermind, solved! I don't know why this was giving me so much trouble. On the offchance anyone else comes this way:

import re

filename = "DJI_0991.MP4"

file_number = re.findall('DJI_(.+?)\W', filename)

print (file_number )
Reply
#2
It probably gave you trouble because of the non-greedy matching. Another possibility is you might have used a control sequence where you wanted a backslash.

Why not grab all the digits instead of looking for the character after the digits?
import re

pattern = re.compile(r'DJI_(\d+)')

matches = re.findall(pattern, 'DJI_0991.MP4 DJI_0992 - testing.MP4 JI_0993 - testing.MP4 DJI 0994.MP4')
 
print(matches)
Output:
['0991', '0992']
Reply
#3
Because sometimes there's other digits in the filenames too, like "DJI_0090 - part 1.MP4"
Reply
#4
I don't understand. In the example below should it print 0090 or not?
import re

pattern = re.compile(r'DJI_(\d+)')

matches = re.findall(pattern, 'DJI_0991.MP4 DJI_0992 - testing.MP4 DJI_0090 - part 1.MP4')
 
print (matches)
Output:
['0991', '0992', '0090']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 782 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,080 Apr-08-2023, 06:36 PM
Last Post: Winfried
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 1,853 Nov-16-2022, 01:49 PM
Last Post: carecavoador
  [SOLVED] Why does regex fail cleaning line? Winfried 5 2,478 Aug-22-2021, 06:59 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

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