tags:

views:

70

answers:

6

Hey Everyone,

I need to search a bunch of files for anything that contains either "tblPayment" or "tblInvoice"

I also want to match any tables named "tblPaymentMethod", "tblInvoiceItem", "tblInvoicePayment"

Anybody care to help me out with how to write a regular expression for this?

Thanks again!

+4  A: 
tbl(Invoice|Payment)+

this will also match tables without the table prefix if you need that flexibility.

Edit: there are several ways to do this explicitly (i.e. tblInvoice|tblPayment|...) but you will have to modify your RegEx every time you add a new table that you want matched. This may be what you want, but I wanted to get you something a little more flexible that you don't have to edit constantly.

Robert Greiner
It's important to note the '/' at the beginning and end is not part of the regular expression itself, it's simply a convention that Perl and Javascript use. For the purpose of being language agnostic, I think they can be omitted.
Steve Wortham
noted and removed.
Robert Greiner
That will likely also match tblInvoiceInvoiceInvoice....
Paul Nathan
@Paul, exactly... he wants to be able to match tables like `tblInvoicePayment` or maybe `tblInvoiceRandom` later and this will take care of that as well.
Robert Greiner
This is perfect!! Thanks!
Jim B
What about `tblInvoiceItem`?
Tim Pietzcker
@Tim yes, it will match the `tblInvoice` part of the table name. In the OP's case, it makes sense to hit on partial matches so you don't have to explicitly add every single table name you want matched on. In other cases, you might want full matches (in which case my RegEx wouldn't work).
Robert Greiner
@Jim B, no problem, I'm glad it did what you are looking for. Good luck!
Robert Greiner
A: 

You need this regular expression.

tblPayment|tblInvoice|tblPaymentMethod|tblInvoiceItem|tblInvoicePayment

Implementation will depend on the language your are implementing it in.

Jason Webb
A: 
tbl(Payment(Method)?|Invoice(Item|Payment)?)

is shortest but probably a bit harder to maintain than simply doing a literal text comparison with a list of strings.

Tim Pietzcker
A: 

In Perl RE syntax:

/tbl
 ((Payment(Method)?)
 |
 (Invoice(Item|Payment)?))
/x

will match what you are looking for.

Paul Nathan
A: 

In python you can use something like this: "tbl(Invoice|Payment)+?"

number5
A: 

If you want to match any table name that starts with tblInvoice or tblPayment then this will do it:

tbl(?:Invoice|Payment)\w*

(?:Invoice|Payment) is a noncapturing group (more efficient in this case than a capturing group). And then \w* will optionally match the alphanumeric characters immediately following.

Steve Wortham