abstractx1
abstractx1

Reputation: 459

Java split string with RegexOption.MULTILINE

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?

Upvotes: 2

Views: 1288

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 44985

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"

Upvotes: 5

Related Questions