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: 2341
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: 30
Joined: Wed Nov 14, 2018 4:32 pm

Re: Getting help with Regular Expressions

Postby pras92 » Thu Oct 01, 2020 10:44 am

Hello,

I'm trying to use RegEx with BRU for the first time and couldn't get to make it work. I'd like to remove everything between two given string, including the two given string.

In the below sample text file, I'd like to remove everything between P and Z
ABC1PQR-XYZ-FG.txt

so it looks like this:
ABC1-FG.txt

I do this with a simple P.*Z in my RegEx editor, but this doesn't seem to work in BRU. What's the simplest way to achieve this?
pras92
 
Posts: 2
Joined: Thu Oct 01, 2020 10:16 am

Re: Getting help with Regular Expressions

Postby pras92 » Thu Oct 01, 2020 11:51 am

pras92 wrote:What's the simplest way to achieve this?


Nevermind. I updated to v3.4.1.0 and saw new options under RegEx (1). With the new v2 option, I'm able to get what I need. :D
pras92
 
Posts: 2
Joined: Thu Oct 01, 2020 10:16 am

Re: Getting help with Regular Expressions

Postby vscopise » Sun Dec 05, 2021 4:35 pm

I have a list of files like that

Diario La R Papel N62 0512 1
Diario La R Papel N62 0512 2
Diario La R Papel N62 0512 3
...
Diario La R Papel N62 0512 10
Diario La R Papel N62 0512 11
Diario La R Papel N62 0512 12
...

I like to rename all the files into:
01N
02N
03N
...
10N
11N
12N
...

Thanks!
vscopise
 
Posts: 1
Joined: Sun Dec 05, 2021 4:30 pm

Match ending 1-or-2 numbers after a very exact name

Postby Luuk » Sun Dec 12, 2021 12:05 pm

There is different ways, but for the filenames that is posted the 'Match' and 'Replace' can be like...
^Diario La R Papel N62 0512 ([1-9])$(?X)^Diario La R Papel N62 0512 (\d\d)$
0$1N(?X)$1N

This does need a checkmark inside of "v2" because its two different regexs.
Luuk
 
Posts: 689
Joined: Fri Feb 21, 2020 10:58 pm

Previous

Return to Regular Expressions


cron