Is there any regex to grab the names from alpha-numeric file name

31
October 25, 2019, at 12:50 PM

I have to write a regex in SQL to filter and capture the file name and exclude the date stamps and the file fromat, is there any way we can do that? the examples I have are-

  • 12345_abcdesfklajf_fjashksjh_20170901_20180831.avc
  • kjfsjal-dklf-lkdhls.bcd
  • ksjsklfj-slkhslkdj-slkdhks-20190401.dhf
  • 3298383-fjha-sakh-sldk-shlksh.kkjj
  • sifhlsk_skjfh_alkfdhi_20180611_20120911_20180909

so here for the first one I just want to capture

  • 12345_abcdesfklajf_fjashksjh_

this part and for the rest, everything before the . or the date stamp.

Answer 1

You can use re.sub() in Python:

prefix = re.sub(r'([-_]\d{6})*(\.\w+)?$', '', filename)

[-_]\d{6} matches an underscore or hyphen followed by 6 digits. Putting this in ()* matches any sequence of them. \.\w+ matches the extension, putting it in ()? makes it optional. And $ makes all this match at the end of the filename.

READ ALSO
Emulate an Intersect in MySQL w/ multiple tables and Join

Emulate an Intersect in MySQL w/ multiple tables and Join

I need to emulate an Intersect query in MySQL

49
Write Pandas DataFrame into a existing MySQL Database Table

Write Pandas DataFrame into a existing MySQL Database Table

I have created a database using phpmyadmin called test that has one table called client_infoThe table in that database is empty (as shown in the attached image)

28
SIGSEV signal recieved printing a result in C with the MySQL API

SIGSEV signal recieved printing a result in C with the MySQL API

After having spent hours on my own trying to solve the problem and asked programming teachers in my university, I'm coming here hoping to find some help

57
Cannot connect to localhost mysql database from mobile - SocketTimeoutException

Cannot connect to localhost mysql database from mobile - SocketTimeoutException

I am trying to connect to my local mysql database (wamp server) from my android device

54