• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular expression - Pattern and matcher

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 9
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79390
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tell me if this works .




 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your great help...
 
avishek bose
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

avishek bose wrote:Thanks a lot for your great help...



was that for me? if yes, you're welcome.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic