Author Topic: Regular expression search Gotscha - to watch out for  (Read 3047 times)

ubacher

  • Oldtimer
  • ****
  • Posts: 2358
Regular expression search Gotscha - to watch out for
« on: December 16, 2013, 02:18:16 PM »
The following expression returns the desired .dng and/or .NEF files

Code: [Select]
Set tmp = Database.GetFiles(img.Name+"(.dng|.NEF)",True)
if I add a | after .NEF like this:
Code: [Select]
Set tmp = Database.GetFiles(img.Name+"(.dng|.NEF|)",True)
it also returns the .jpg file i.e. img

Not sure if that is legit or a bug.

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 29760
Re: Regular expression search Gotscha - to watch out for
« Reply #1 on: December 16, 2013, 03:32:58 PM »
Tip: You can try out regular expressions with the Regular Expression Tester script.

For your expression, which includes an empty expression as an alternative. the regular expression tester shows that your expression matches all terms which start with the file name you specify. The regular expression is valid and is processed correctly. But it does of course not return what you expect.

Basically your regular expression is:

FileName.

and this matches everything starting with Filename.

ubacher

  • Oldtimer
  • ****
  • Posts: 2358
Re: Regular expression search Gotscha - to watch out for
« Reply #2 on: December 16, 2013, 11:52:34 PM »
Problem was that I had removed another term that was after the | and in doing so I forgot to remove the trailing |.


lenmerkel

  • Full Member
  • **
  • Posts: 107
  • Don't Panic!
Re: Regular expression search Gotscha - to watch out for
« Reply #3 on: January 17, 2014, 07:32:11 PM »
Regular expressions can be tricky, but oh so powerful! I use them regularly in Java and .Net development.

Here's my personal favorite (and free) 'try-it-and-see' regex test website: http://www.regexr.com/.

It allows you try out pretty much any regex (in match or replace mode), and single-step through the matching process.
I typically build and try out an expression here first, get it working, then copy and past the tested expression into my own code.
Over the hill, and enjoying the glide.