2

I want to split for example the following Sql statements by semi-colon end of line:

CREATE TABLE projects(
   id INTEGER PRIMARY KEY AUTOINCREMENT     NOT NULL,
   name           TEXT    NOT NULL,
   created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX ix_tasks_project_id ON tasks (project_id);

SELECT * FROM projects WHERE name = "someName;WithSemiColon";

Something like: string.split(";$"); (but with RegexOption.MULTILINE applied)

Can someone please explain how do I apply the RegexOption?

9
  • if its posted like this you can simply split at ; with no need of regex or anything else Commented Jan 17, 2017 at 12:18
  • Thanks. This is just an example. I want to cater for when ; appears within sql statements as well. I'm looking to split sql statements using a reliable regex. Commented Jan 17, 2017 at 12:20
  • @jack jay: where did you read this? sqlite.org/autoinc.html Commented Jan 17, 2017 at 12:28
  • i thought it was MySql but thanks @abstractx1 Commented Jan 17, 2017 at 12:30
  • @jack Ah ok, no worries. Thanks. Commented Jan 17, 2017 at 12:32

1 Answer 1

5

Simply prefix your regular expression with (?m) to enable the flag MULTILINE so in your case it would be (?m);$

for (String s : string.split("(?m);$")) {
    System.out.printf("----> %s%n", s.trim());
}

Output:

----> CREATE TABLE projects(
   id INTEGER PRIMARY KEY AUTOINCREMENT     NOT NULL,
   name           TEXT    NOT NULL,
   created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)
----> CREATE INDEX ix_tasks_project_id ON tasks (project_id)
----> SELECT * FROM projects WHERE name = "someName;WithSemiColon"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but this does not seem to split the string.
I apologize. You're right. After looking further into the parser, the newlines were being removed. Thank you so much for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.