views:

303

answers:

2

I'm not an expert on regular expressions so I thought I'd throw this question out. I'm thinking regular expressions can help make my tests more robust (testing generated EJB QL statements for correct generation).

For example:

select u.firstName, u.lastName from Users u where u.age > 50

What regex do I use to split it into the following?

  1. "u.firstName, u.lastName"
  2. "Users u"
  3. "u.age > 50"

regardless of the case of the keywords (i.e. SELECT, Select, select, ...) and regardless of the possible spaces before and after the keywords? Of course it would be even better to split it further, but if I can split parts as above example, I can improve my tests.

Thanks in advance for any pointers and help.

+1  A: 

The regex to split would be something like:

 select|from|where

you can use the lanaguage s tool to compile regex to be non case sensitive

//Example in java
Pattern.compile("select|from|where",Pattern.CASE_INSENSITIVE).split("select x from y where z)
Jass
+1  A: 

The easiest regex would be something like:

select(.*?)from(.*?)where(.*)

Depending on your language you should be able to set flags for regex to be case insensitive. The result of it will be 3 captured groups that contain required information (surrounded by spaces). It would be easier just to use trim() or similar function in your language to strip unneded spaces rather than making regex more complex.

serg