开发者

Simple Regex Question

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

I also wan开发者_StackOverflowt 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!


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.


You need this regular expression.

tblPayment|tblInvoice|tblPaymentMethod|tblInvoiceItem|tblInvoicePayment

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


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.


In Perl RE syntax:

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

will match what you are looking for.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜