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 6596865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:07:58+00:00 2026-05-25T18:07:58+00:00

I have a Java String which is actually an SQL script. CREATE OR REPLACE

  • 0

I have a Java String which is actually an SQL script.

CREATE OR REPLACE PROCEDURE Proc
   AS
        b NUMBER:=3;
        c VARCHAR2(2000);
    begin
        c := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;';
   end Proc;

I want to split the script on semi-colon except those that appear inside a string.
The desired output is four different strings as mentioned below

1- CREATE OR REPLACE PROCEDURE Proc AS b NUMBER:=3
2- c VARCHAR2(2000)
3- begin c := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;';
4- end Proc

Java Split() method will split above string into tokens as well. I want to keep this string as it is as the semi-colons are inside quotes.

c := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;';

Java Split() method output

1- c := 'BEGIN ' || ' :1 := :1 + :2
2- ' || 'END
3- '

Please suggest a RegEx that could split the string on semi-colons except those that come inside string.

===================== CASE-2 ========================

Above Section has been answered and its working

Here is another more complex case

======================================================

I have an SQL Script and I want to tokenize each SQL query. Each SQL query is separated by either semi-colon(;) or forward slash(/).

1- I want to escape semi colon or / sign if they appear inside a string like

...WHERE col1 = 'some ; name/' ..

2- Expression must also escape any multiline comment syntax which is /*

Here is the input

/*Query 1*/
SELECT
*
FROM  tab t
WHERE (t.col1 in (1, 3)
            and t.col2 IN (1,5,8,9,10,11,20,21,
                                     22,23,24,/*Reaffirmed*/
                                     25,26,27,28,29,30,
                                     35,/*carnival*/
                                     75,76,77,78,79,
                                     80,81,82, /*Damark accounts*/
                                     84,85,87,88,90))
;
/*Query 2*/    
select * from table
/
/*Query 3*/
select col form tab2
;
/*Query 4*/
select col2 from tab3 /*this is a multi line comment*/
/

Desired Result

[1]: /*Query 1*/
    SELECT
    *
    FROM  tab t
    WHERE (t.col1 in (1, 3)
                and t.col2 IN (1,5,8,9,10,11,20,21,
                                         22,23,24,/*Reaffirmed*/
                                         25,26,27,28,29,30,
                                         35,/*carnival*/
                                         75,76,77,78,79,
                                         80,81,82, /*Damark accounts*/
                                         84,85,87,88,90))

[2]:/*Query 2*/    
    select * from table

[3]: /*Query 3*/
    select col form tab2

[4]:/*Query 4*/
    select col2 from tab3 /*this is a multi line comment*/

Half of it can already be achieved by what was suggested to me in the previous post( link a start) but when comments syntax(/*) is introduced into the queries and each query can also be separated by forward slash(/), expression doesn’t work.

  • 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-05-25T18:07:59+00:00Added an answer on May 25, 2026 at 6:07 pm

    The regular expression pattern ((?:(?:'[^']*')|[^;])*); should give you what you need. Use a while loop and Matcher.find() to extract all the SQL statements. Something like:

    Pattern p = Pattern.compile("((?:(?:'[^']*')|[^;])*);";);
    Matcher m = p.matcher(s);
    int cnt = 0;
    while (m.find()) {
        System.out.println(++cnt + ": " + m.group(1));
    }
    

    Using the sample SQL you provided, will output:

    1: CREATE OR REPLACE PROCEDURE Proc
       AS
            b NUMBER:=3
    2: 
            c VARCHAR2(2000)
    3: 
        begin
            c := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;'
    4: 
       end Proc
    

    If you want to get the terminating ;, use m.group(0) instead of m.group(1).

    For more information on regular expressions, see the Pattern JavaDoc and this great reference. Here’s a synopsis of the pattern:

    (              Start capturing group
      (?:          Start non-capturing group
        (?:        Start non-capturing group
          '        Match the literal character '
          [^']     Match a single character that is not '
          *        Greedily match the previous atom zero or more times
          '        Match the literal character '
        )          End non-capturing group
        |          Match either the previous or the next atom
        [^;]       Match a single character that is not ;
      )            End non-capturing group
      *            Greedily match the previous atom zero or more times
    )              End capturing group
    ;              Match the literal character ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java string which looks like this, it is actually an XML
I have a java string, which has a variable length. I need to put
I have this java string: String bla = <my:string>invalid_content</my:string>; How can I replace the
I have to replace the content of this xml string through java <My:tag>value_1 22&#xA;value_2
I have encountered some java script code which I believe is malicious but most
I have two strings in a java program, which I want to mix in
I have a Java string of XML content. I use Velocity to generate some
I have tried &nbsp;&nbsp; to display two spaces in a standard output Java String.
I have a Java application that's very String-heavy - it takes a feed of
I have a Java Properties object that I load from an in-memory String ,

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.