• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression - Pattern and matcher

 
avishek bose
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Harsha Smith
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell me if this works .




 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your great help...
 
avishek bose
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps it's a better idea to use an SQL parser. Zql can parse SQL statements. From the little experience I have:
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avishek bose wrote:Thanks a lot for your great help...


was that for me? if yes, you're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic