Can't get Regex to work

Post any Bulk Rename Utility support requirements here. Open to all registered users.

Can't get Regex to work

Postby mcgern » Sat Aug 19, 2006 8:53 pm

Hi,

Love the program, it is very handy, but I seem to be having problems with the Regex functionality. I have done searches on the forum, but can't find an answer.

I am suitably confident with using regular expressions themselves, it is just that I seem to be missing something at making them work correctly.

What seems to happening is that if *any* regular expression is matched, the entire file name is replaced with the replace string.
e.g. test using a simple regex

file :- "some_test_file.txt"

match :- "some"
replace :- "another"

result :- "another.txt"
expected :- "another_test_file.txt"

Sorry, but I couldn't find anything else on the subject, so therefore it must be user error, and I hope I haven't wasted to much time.

Using version 2.3.7.2 on WinXp sp2.

Thanks,

Jarrod
mcgern
 
Posts: 3
Joined: Mon Aug 14, 2006 8:35 pm

Postby Jane » Sun Aug 20, 2006 6:30 am

Try saving the unmatched portion that you want to retain in a captured group

Match: some(.*)
Replace: another\1

Then
some_text_file.txt
will become
another_text_file.txt

It works fine on mine.
If you want to replace a different section within the filename rather than at the start, save the sections before and after in 2 different groups.

Good luck,
Jane
Jane
 
Posts: 24
Joined: Sat Aug 05, 2006 1:20 am

Postby mcgern » Sun Aug 20, 2006 8:55 pm

Thanks Jane.

This method seems to work fine. I was just more used to only finding and replacing the particular text that was needed, not having to also find out the bits that don't need replaced.

I know most things could be handled the normal replace (3) functionality, but what if I wanted replace all instances of a particular match and not know how many items where in the string? Or is that me just being picky?

example:- replace all instances of t.{1,2}st with TEST
test_toast_trust

(sorry this is a crap example, but I couldn't think of anything better). also how does the "g" and "i" functionality fit into the BRU regex model?

Thanks for any more help.

Sorry for any time wastage as this should probably now be in the regular expression forum.

Cheers,

Jarrod
mcgern
 
Posts: 3
Joined: Mon Aug 14, 2006 8:35 pm

Postby Jane » Sun Aug 20, 2006 11:19 pm

I think your t.{1,2}st example will work if you again try
(.*)t.{1,2}st(.*)
and capture the prefix and suffix in 2 groups.

Search:
(.*)t.{1,2}st(.*)

Replace:
\1test\2

However it doesn't seem to have a global "g" option., so you can't seem to replace them all in one pass. You can cycle through them with (.*)t.{1,2}st(.*) and each time it will make a further replacement for additional matches.

The case insensitive "i" option used to be available as a check box, but it didn't work so Jim removed it.

Jane
Jane
 
Posts: 24
Joined: Sat Aug 05, 2006 1:20 am

Postby Glenn » Sun Aug 20, 2006 11:40 pm

I know if this gets out Jim will have to kill me, but I was also frustrated by lack of case insensitive searching, so I did some research, and I found that you can make the search case insensitive i.e. "i" - by putting (?i) in front of your regular search criteria.

In the example above:

(.*)t.{1,2}st(.*)

you would change the search criteria to:

(?i)(.*)t.{1,2}st(.*)

The PCRE engine uses that (?i) to toggle case insensitivity and it is not a capturing group and in no other way affects the search.

Cordially,
Glenn
Glenn
 
Posts: 28
Joined: Fri Apr 14, 2006 4:53 pm
Location: Winnipeg, Canada

Postby Admin » Mon Aug 21, 2006 8:57 am

Thanks Glenn. Yes, I use the PCRE engine, so http://www.pcre.org is always a good starting place for documentation. BRU uses very little RE logic, it simply passes everything to the PCRE file and interrogates the results.

Oh, and no need to kill you :wink:



Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby mcgern » Mon Aug 21, 2006 2:42 pm

Thanks everyone for the replies.

And thank you Jim for such a great program!
mcgern
 
Posts: 3
Joined: Mon Aug 14, 2006 8:35 pm


Return to BRU Support