Splitting single file to multiple file - java

I was given a SQL file that consists of more than 20,000 lines. The sql consists of procedures, ddl and dml. I finding a 'faster' way to split the file...
So I was thinking of creating a script that split the content according to the functions. Below are my regex:
String sp_regex = "(CREATE OR REPLACE PROCEDURE) .+(SHOW ERRORS;)$";
String insert_regex = "(INSERT INTO) .+(SHOW ERRORS;)$";
String delete_regex = "(DELETE FROM) .+([)];)$";
String table_regex = "(CREATE TABLE) .+([)];)$";
But none of the regex working. The content didn't split at all. What is the problem with my regex?
Sample SQL:
CREATE OR REPLACE PROCEDURE "SCHEMA"."SP" (
......
);
/
SHOW ERRORS;
CREATE TABLE "SCHEMA"."TABLE" (
......
);
INSERT INTO "SCHEMA"."TABLE" ( ...... ) VALUES ( "......" );
DELETE FROM "SCHEMA"."TABLE" WHERE ..... = "....";

You should allow the . to match newlines and allow the $ to match line endings (not just EOF). If you do that, you will also need to make the + quantifier lazy in order to tell it to stop matching at the earliest possible point:
String sp_regex = "(?sm)(CREATE OR REPLACE PROCEDURE) .+?(SHOW ERRORS;)$";
String insert_regex = "(?sm)(INSERT INTO) .+?(SHOW ERRORS;)$";
String delete_regex = "(?sm)(DELETE FROM) .+?([)];)$";
String table_regex = "(?sm)(CREATE TABLE) .+?([)];)$";
You need to test this thoroughly - lines that don't match any of these regexes will simply be ignored.

Related

Regex - get second word after first match

I'm trying to parse a simple DDL statement. First I'm trying to pull the table name out.
The syntax will be something like 'CREATE TABLE DB_NAME.TABLE_NAME'
So far I've got this:
String line = "CREATE TABLE DB_NAME.T_NAME";
String pattern = ".*?\\bTABLE\\s+(\\w+)\\b.*";
System.out.println(line.replaceFirst(pattern, "$1"));
That gives me back "DB_NAME". How can I get it to give me back "T_NAME"?
I tried following the update in this answer, but I couldn't get it to work, probably due to my very limited regex skills.
What about sth like this:
.*?\\bTABLE\\s+\\w+\\.(\\w+)\\b.*
Demo
It first matches the TABLE keyword with .*?\\bTABLE\\s+. Then it matches DB_NAME. with \\w+\\.. Finally it matches and captures T_NAME with (\\w+)
Here's a small piece of code that will do (using named capturing groups):
String line = "CREATE TABLE DB_NAME.T_NAME";
Pattern pattern = Pattern.compile("CREATE TABLE (?<database>\\w+)\\.(?<table>\\w+)");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
String database = matcher.group("database"); // DB_NAME
String table = matcher.group("table"); // T_NAME
}
You may extract all the string after the TABLE into a group and then split with comma to get individual values:
String line = "CREATE TABLE DB_NAME.T_NAME";
String pattern = "\\bTABLE\\s+(\\w+(?:\\.\\w+)*)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(line);
if (m.find()){
System.out.println(Arrays.toString(m.group(1).split("\\.")));
// => [DB_NAME, T_NAME]
}
See the Java demo.
If you are sure of the incoming format of the string, you might even use
"\\bTABLE\\s+(\\S+)"
See another Java demo.
While \w+(?:\.\w+)* matches 1+ word chars followed with 0+ repetitions of . and 1+ word chars, \S+ plainly matches 1+ non-whitespace chars.

How to exchange the space for backslash and a double quote using replace in Java

I'm coding a Java app to store paths in a particular format, so I need to escape some characters in order to put the paths in a database, but I cannot do it properly:
The original string looks like this:
ML Database Prototype\\NAS-500\\
and I need it in this particular format:
"\"ML\ Database\ Prototype\\NAS-500\""
So far I'm trying to do it using
String str = "ML Database Prototype\\NAS-500\\";
newStr = ( "\"\""+str+"\"" ).replace(" ","\" ");
System.out.println(newStr);
""WT" Database" Prototype\\DR0151-populated"
You can use as follows and will work:
newStr = ( "\"\\\""+str+"\\\"\"" ).replace(" ","\\ ");
The output for this is:
"\"ML\ Database\ Prototype\\NAS-500\""

how to split a string which contains of ( \n : , .)

so how can split this combination in android ?
Thanks in advance :)`
i'm trying like thatString lines[] = String.split("\\r?\\n", -1);
but how can split all data in one time
You can use Pattern for regex split
String fields = "name[Employee Name], employeeno[Employee No], dob[Date of
Birth], joindate[Date of Joining]";
Pattern pattern = Pattern.compile("\\[.+\\]+?,?\\s*" );
String[] split = pattern.split(fields);
References: How to split this string using Java Regular Expressions

Removing new Line from sql record via Java

I am reading and manipulating a MS SQL table using JPA. I have text in the cells that I want to cleanup by removing line breaks. Below is one sample of the text (pasting this in notepad++ shows **CR LF** on each line) :
(
(NVSM in (1,2)) and
(NISFVSM in (1,2)) and
(TRMBVSM = 0)
)
I have tried the following code but I can not get rid of the newlines
flatTextString.trim()
.replace(System.getProperty("line.separator"), " ")
.replaceAll("\t", "")
.replaceAll("(\\r|\\n)", "")
.replaceAll("\\s{2,}", " ")
;
How can I fix this?
I suggest just replacing all newline and carriage returns with just a space, and then remove all whitespace before closing ) and after opening (:
String flatTextString = "(\r\n(NVSM in (1,2)) and\r\n(NISFVSM in (1,2)) and\r\n(TRMBVSM = 0)\r\n)";
System.out.println(flatTextString.replaceAll("[\r\n]+", " ").replaceAll("\\s+\\)", ")").replaceAll("\\(\\s+", "(")); // My way
// => ((NVSM in (1,2)) and (NISFVSM in (1,2)) and (TRMBVSM = 0))
See IDEONE demo
As Java regex cannot use conditional replacement patterns, you can only chain replaceAll methods as I have shown in the code snippet above.

java code how to read null values in csv file

I have csv in file containing multiple rows.If first column value is nothing its is giving error and m not able to insert in database.
ex
If row is :130,1,datafile8.csv, 2007 ,17,List_date no problem in reading n inserting
but if row is: ,0,datafile8.csv,Bihar,7,list_Left ,not able to read n insert .how to insert null in above row .so i can insert dis row in database.
String keyword = "celldescription.csv";
File makefile = new File(keyword);
BufferedReader r2 = new BufferedReader(new FileReader(makefile));
strLine1 = r2.readLine();
System.out.println (strLine1);
String r="0";int r1=0;
while((strLine1=r2.readLine())!=null)
{
System.out.println (strLine1);
StringTokenizer st2 = new StringTokenizer(strLine1, ",");
// Print the content on the console
String cellvalue = st2.nextToken();
String position = st2.nextToken();
String Docid=st2.nextToken();
String Word=st2.nextToken();
String Count=st2.nextToken();
String List_Entry=st2.nextToken();
String tab3="insert into description(cellvalue,position,Docid,Word,Count,List_Entry) values(?,?,?,?,?,?)";
ps = connection.prepareStatement(tab3);
ps.setString (1,cellvalue );
ps.setString (2,position );
ps.setString (3,Docid);
ps.setString (4,Word );
ps.setString (5,Count );
ps.setString (6,List_Entry );
ps.executeUpdate();
}//end of while
r2.close();
System.out.println("Data is inserted");
}//try closed**
When your String strLine1 starts with comma(,) StringTokenizer omit empty string if it is in start or end or even in between.
Ex - ,0,datafile8.csv,Bihar,7,list_Left
token -> "0" - "datafile8.csv" - "Bihar" - "7" and "list_Left"
better you split the string by comma(,).
Ex -
String[] str = strLine1.split(",",-1);
str[] -> ["","datafile8.csv","Bihar","7" and "list_Left"]
You may want to consider using a java library for your work with csv files.
OpenCSV is one, it helped me a lot.
Some of its features:
Arbitrary numbers of values per line
Ignoring commas in quoted elements
Handling quoted entries with embedded carriage returns (ie entries that span multiple lines)
Configurable separator and quote characters (or use sensible defaults)
Read all the entries at once, or use an Iterator style model
Creating csv files from String[] (ie. automatic escaping of embedded quote chars)

Categories

Resources