Page 1 of 1

Can't get Regex to work

PostPosted: Sat Aug 19, 2006 8:53 pm
by mcgern
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

PostPosted: Sun Aug 20, 2006 6:30 am
by Jane
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

PostPosted: Sun Aug 20, 2006 8:55 pm
by mcgern
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

PostPosted: Sun Aug 20, 2006 11:19 pm
by Jane
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

PostPosted: Sun Aug 20, 2006 11:40 pm
by Glenn
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

PostPosted: Mon Aug 21, 2006 8:57 am
by Admin
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

PostPosted: Mon Aug 21, 2006 2:42 pm
by mcgern
Thanks everyone for the replies.

And thank you Jim for such a great program!