I'm trying to parse this string as follows:
CREATE TABLE tbl_1 (a1 int, a2 varchar(20));
To
CREATE TABLE
tbl_1 (a1 int, a2 varchar(20));
How would I go about doing this with a delimiter in JAVA?
I am able to parse:
CREATE DATABASE db_1;
to
CREATE DATABASE
db_1;
with this code but it doesn't work for the statement at the beginning of the post.
public static void read()
{
Scanner scanner = new Scanner(System.in);
System.out.print("-->");
String input = scanner.nextLine();
String [] num = new String[10];
Scanner parse = new Scanner(input);
parse.useDelimiter("\s((?=[a-z][a-z]))");
int i = 0;
while (parse.hasNext())
{
num[i] = parse.next();
System.out.println("Output " i " is: " num[i]);
if(num[i].contains("CREATE DATABASE"))
{
System.out.println("YOOOOOOO");
}
i ;
}
CodePudding user response:
Switch to this regEx:
parse.useDelimiter("(?=tbl)");
You can pass whatever delimiter you want too, with:
String delimiter = "tbl";
parse.useDelimiter("(?=" delimit ")");
This find the first occurrence of a whitespace before the String
you want.
CodePudding user response:
It can be done properly using a different approach, but using your approach of a scanner delimiter:
parse.useDelimiter("(?<=[A-Z])\\s");
This splits on spaces that are preceded by a capital letter.
Not exactly correct in all cases, but should work well for your case of parsing SHOUTED KEYWORDS formatted SQL.