Getting help with Regular Expressions

A swapping-ground for Regular Expression syntax

Re: Getting help with Regular Expressions

Postby Admin » Fri Oct 18, 2019 8:44 am

You can do that with Replace(3) and Remove(5) instead of RegEx.
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Getting help with Regular Expressions

Postby kgng97 » Sat May 16, 2020 9:36 am

I am relatively new to the bulk rename utility. I would like to do the following:
Rename: Lastname AB_LCH clin_2019-07-26A_3_Blood test report_ocr
To: Lastname AB_2019-07-26A_LCH clin_3_Blood test report_ocr

That is, move the second group of text (LCH clin) to after the date.

Under RegEx (1), I tried to make 4 groups, the first 3 each ending with an underscore (_), as follows:
Match: ^(.*_)(.*_)(.*_)(.*)
Replace: \1\3\2\4

This doesn't work, and I have not been able to figure out how to get it to work.
I will be grateful if someone can help me. Thank you.
kgng97
 
Posts: 2
Joined: Sat May 16, 2020 9:05 am

Re: Getting help with Regular Expressions

Postby RegexNinja » Mon May 18, 2020 7:16 am

kgng97:
Without seeing more examples, its hard to advise just one specific regex.
Here's some matches to work on the example, in order of how many names they could match.

Start with the bottom match, then work your way upwards (if you need to match more names).
If you need help making any of the groups more specific/relaxed, just post back with details.
Btw .*?_ matches the 1st-underscore, while .*_ matches the last-underscore, so you were almost there.
Cheers!

Matches:
^(.*?)(_.*?)(_.*?)(_.*)$
^(.*?)(_.*?)(_.*?[A-Z])(_.*)$
^(.*?)(_.*?)(_.*?\d[A-Z])(_.*)$
^(.+?)(_.+? .+?)(_.*?[\d][A-Z])(_.+)$
^(.+?)(_[A-Z]{2,5} .+?)(_.*?[\d][A-Z])(_.+)$
^(.+? [A-Z]{1,3})(_[A-Z]{2,5} .+?)(_\d{4}-\d{2}-[\d]{2}[A-Z])(_.+)$
^(.+? [A-Z]{2})(_[A-Z]{3} .+?)(_(?:19\d\d|20\d\d)-\d{2}-[\d]{2}[A-Z])(_.+)$
^(.+? [A-Z]{2})(_[A-Z]{3} .+?)(_(?:20[0-2]|19[7-9])\d-\d{2}-\d{2}[A-Z])(_.+)$
^(.+? [A-Z]{2})(_[A-Z]{3} [a-z]+?)(_(?:20[0-2]|19[7-9])\d-[01]\d-[0-3]\d[A-Z])(_\d_.+)$

Replace:
\1\3\2\4

Result:
Lastname AB_LCH clin_2019-07-26A_3_Blood test report_ocr ---> Lastname AB_2019-07-26A_LCH clin_3_Blood test report_ocr
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Getting help with Regular Expressions

Postby kgng97 » Sun Jun 14, 2020 11:46 am

Thank you, RegexNinja, for your quick response. Sorry for this late acknowledgment. After studying your various options, I got a better understanding of how to bulk-rename files and managed to make them work for my files. Thanks again.
kgng97
 
Posts: 2
Joined: Sat May 16, 2020 9:05 am

Re: Getting help with Regular Expressions

Postby Jack2 » Mon Aug 10, 2020 4:19 am

I found this useful 'beginner' level guide to Regex by Calibre :
.https://manual.calibre-ebook.com/regexp.html#first-a-word-of-warning-and-a-word-of-courage
Jack2
 
Posts: 15
Joined: Wed Nov 14, 2018 4:32 pm

Previous

Return to Regular Expressions