| Author |
Regular expression - Pattern and matcher
|
avishek bose
Greenhorn
Joined: Oct 12, 2011
Posts: 15
|
|
hi,
I want to use a regular expression in java to get the column names from a query string -
ex- select NVL(SUM(EMP_SAL), 0), MAX(EMP_AGE), EMP_NAME, DEPT_NAME, NVL(DEPT_ID, "") from EMP_TABLE
Can anyone help me with the regular expression(to be used in pattern and matcher ) to capture only the column names like EMP_SAL, EMP_AGE, EMP_NAME, DEPT_NAME, DEPT_ID
Regards
|
 |
Vikas Solegaonkar
Greenhorn
Joined: Oct 17, 2011
Posts: 8
|
|
Could not get anything better than this.
Output:
EMP_NAME
DEPT_NAME
EMP_SAL
EMP_AGE
DEPT_ID
You can of course merge the three patterns into one.. but the expression will be too ugly to look at.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4750
|
|
avishek dasdsa wrote:I want to use a regular expression in java to get the column names from a query string -
ex- select NVL(SUM(EMP_SAL), 0), MAX(EMP_AGE), EMP_NAME, DEPT_NAME, NVL(DEPT_ID, "") from EMP_TABLE
I guess my question is why? If the statement is valid, the ResultSet will contain all that information. Creating a regex to parse a statement that already has a parser built into your database seems like re-inventing the wheel to me.
But if it's just a regex exercise for fun, forget what I said.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
Welcome to the Ranch
|
 |
Harsha Smith
Ranch Hand
Joined: Jul 18, 2011
Posts: 287
|
|
Tell me if this works .
|
 |
Martin Vajsar
Bartender
Joined: Aug 22, 2010
Posts: 2331
|
|
Winston Gutkowski wrote:I guess my question is why?
I guess that someone wants to track Java code dependencies on database schema objects.
|
 |
avishek bose
Greenhorn
Joined: Oct 12, 2011
Posts: 15
|
|
Thanks a lot for your great help...
|
 |
avishek bose
Greenhorn
Joined: Oct 12, 2011
Posts: 15
|
|
|
hi Winston .. we require it as we want to validate the column names are proper before firing the query in database as these queries are taken from a xml file where the queries are modified by end users. If column names are proper then the query will be executed at the DB end. else it will redirect to error.jsp with a proper message.
|
 |
Martin Vajsar
Bartender
Joined: Aug 22, 2010
Posts: 2331
|
|
avishek bose wrote:hi Winston .. we require it as we want to validate the column names are proper before firing the query in database as these queries are taken from a xml file where the queries are modified by end users. If column names are proper then the query will be executed at the DB end. else it will redirect to error.jsp with a proper message.
Do you realize that you're exposed to the SQL injection attacks in the most serious way imaginable? Your end users might wreak havoc in the database even inadvertently; if there is ever a malicious user, you're going to have tough time.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Perhaps it's a better idea to use an SQL parser. Zql can parse SQL statements. From the little experience I have:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Harsha Smith
Ranch Hand
Joined: Jul 18, 2011
Posts: 287
|
|
avishek bose wrote:Thanks a lot for your great help...
was that for me? if yes, you're welcome.
|
 |
 |
|
|
subject: Regular expression - Pattern and matcher
|
|
|