Home > front end >  Need a regex to find a number and text in a filename
Need a regex to find a number and text in a filename

Time:01-04

Have filenames in the format: 021-05-05_10-10-12-111_Nancy_Test_123456-1234_194456454390816_OD_2021042911270.pdf
I need to find “123456-1234” and OD.

In the 123456-1234 number, the ‘-’ are wildcards so the number can be eg. 1234561234, 123456**1234, 123456_1234 - but there will always be 10 digits. (0-9) and the wildcard (if any) will be between the 6'th and 7'th digit. The “OD” can be “OD” or “OS”, ignore case.

The number and OD/OS must be moved to the beginning of the filename with a server name in between, and today's date after OD/OS to uppercase.

Eg: 123456-1234_servername1_OD_yyyy_mm_dd_ss_021-05-05_10-10-12-111_Nancy_Test_194456454390816_2021042911270.pdf.

I'm using a file renaming program that will take the regex.
(Don't know if advertising is allowed at StackOverflow, if it is, I will of course provide a link to the program).

This is what I got so far:
(?:_od_|_sd_) gives me the OD or SD

(?<!\d)\d{10}(?!\d) gives me the 1234561234 but only if there are no wildcards between the 6'th and 7'th digit.

Furthermore, I can't figure out how to put them together and move them in front with the server name in between.

CodePudding user response:

You can use

(?i)^(.*?)_(\d{6}\D*\d{4})_(\d )_(od|sd)_

Replace with $2_servername1_$4_yyyy_mm_dd_ss_$1_$3_.

See the regex demo. Details:

  • (?i) - case insensitive mode on
  • ^ - start of string
  • (.*?) - Group 1: any zero or more chars other than line break chars as few as possible
  • _ - an underscore
  • (\d{6}\D*\d{4}) - Group 2: six digits, zero or more non-digits, four digits
  • _ - an underscore
  • (\d ) - Group 3: one or more digits
  • _ - an underscore
  • (od|sd) - Group 4: od or sd
  • _ - an underscore
  •  Tags:  
  • Related