简体   繁体   中英

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 ?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM