问题
I am reading a pl/sql code from a text file and storing all words of it into array list from below code :
Scanner in1 = new Scanner(file1);
ArrayList<String> Code1 = new ArrayList<String>();
in1.useDelimiter("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|[\\p{javaWhitespace}\\.|,]+");
while (in1.hasNext())
{
Code1.add(in1.next().toLowerCase());
}
Everything is working fine but i am facing issue whenever there is a comments section in code written in after special character -- Like below:
select * from
Dummy_Table --This is a, dummy.table
where id = 1 -- Filter.on, id
For above code i don't want to store the comments (--This is a, dummy.table) and (-- Filter.on, id) in my list.How can i do this ?
in1.useDelimiter("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|[\\p{javaWhitespace}\\.|,]+");
I am using above delimiter to skip reading comment section enclosed between /* and */, which is multiple line comments as written below but including this i also want to skip reading/storing the single statement comments i.e. after -- till the end of line.
/*
|| This is a comments section in pl/sql code...||
|| Which i don't want to store.. ||
*/
回答1:
You can add part after --
till end of line in your regex like this:
in1.useDelimiter("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|--[^\\n]*|[\\p{javaWhitespace}\\.|,]+");
RegEx Demo
回答2:
For removing inline comments, have you considered using the simple combination of indexOf()
and substring()
? Also, should you be using in1.nextLine()
instead of in1.next()
? For example:
while (in1.hasNext())
{
String line = in1.nextLine();
int indexOfComment = line.indexOf("--");
if (indexOfComment > -1) {
line = line.substring(0, indexOfComment);
}
Code1.add(line.toLowerCase());
}
来源:https://stackoverflow.com/questions/37631426/how-to-prevent-storing-of-text-coming-after-special-characters-in-arraylist-i