Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:31:11+00:00 2026-06-06T02:31:11+00:00

I am working on some application for my masters thesis and in the process

  • 0

I am working on some application for my masters thesis and in the process I have to build a SQL Parser. To do so I’ve decide to go heavy on regexing since it seems the best way at the time.

The problem is that i have some minor problems with my regexes.

Considering some query examples such as:

select 
    RIC 
from 
    (select 
         s.RIC, m.NAME 
     from 
         Stock s, Market m 
     where 
         s.LISTED_ON_EXCHANGE = m.RIC) t 
where 
    RIC > 'G';

select * 
from Stock 
order by COMPANY 
LIMIT 0,2;

select 1+2;

select now();

select 
    s.RIC, m.NAME 
from 
    Stock s 
INNER JOIN  
    Market ON m I s.LISTED_ON_EXCHANGE = m.RIC;

select * 
from Stock 
order by COMPANY;

select * 
from Stock 
where RIC in ('GS.N' , 'INFY.BO');

select * 
from Stock 
where RIC LIKE 'V%';

select * 
from Stock 
where RIC BETWEEN 'G' AND 'I';

select count(*) 
from STOCK 
where LISTED_ON_EXCHANGE IS NOT NULL;

select na_me as n, price as p 
from bla, blabla, blalalaa;

And given the following two regexes:

SELECT_FIELDS_PATTERN = "(?<=[SELECT]) [\\d\\w',.*() ]+ (?=FROM)";

That should match selection fields.

And:

SELECT_FROM_PATTERN = "(?<=[FROM]) [\\w, ]+ (?(?=(?:WHERE|INNER|ORDER)))";

That should match FROM clauses excluding any conditions or ordering etc.

All of the queries except

select 1+2;
select now();

Should be valid. That’s because I only want to parse select queries that contain relevant information for me.

The problem is that the two regexes I’ve created won’t validate for example the last query:

select na_me as n, price as p from bla, blabla, blalalaa;

So I would require some help to improve my regexing for select queries, maybe even merge the two regexes?

An example of a correct output for the first query:

select RIC from (select s.RIC, m.NAME from Stock s, Market m where s.LISTED_ON_EXCHANGE=m.RIC) t where RIC > 'G';

The output should be:

RIC

for the first part and

(select s.RIC, m.NAME from Stock s, Market m where s.LISTED_ON_EXCHANGE=m.RIC) t

for the second part

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:31:12+00:00Added an answer on June 6, 2026 at 2:31 am
    • Character classes are not groups — remove [ & ] around keywords.
    • Don’t use useless lookarounds, it can lead to problems in some cases.
    • You probably want to use \b around keywords so that SELECT does not match in FOOSELECT.
    • Can use (?i) to make the expression case insensitive.

    You could use something like:

    (?i)\bSELECT\b\s+(.+)\s+\bFROM\b\s+([\w\s,]+?)(?:\s+\b(?:WHERE|INNER|ORDER)\b|;?$)
    

    With the parts of interest being captured in the first and second capturing group.

    Note this will not work right with strings and in other cases, also SQL is recursive, which is pretty difficult to parse with Java regex. I suggest you use a proper parser if you want to parse SQL properly. (You can write a simple one your self, using regex for lexing generating tokens and Java to parse the tokens and build a parse tree.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one web application which I have been working for some time.I am
Good evening, I am working on a program where some application config info is
When working on some Javascript for a web application, I noticed that I had
First some background: I'm working on an application and I'm trying to follow MVVM
I'm working on an application that contains some buttons defined via layout.xml like this
I'm working on a console application that tries to download some files from a
I m working on a console application that tries to download some files from
i m working on a web application that reads files from some where and
I'm working in an application (C#) that applies some readability formulas to a text,
I am working on a Java application that will use some Hibernate (annotated by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.