How to prevent storing of text coming after special characters — in arraylist in java

眉间皱痕 提交于 2020-01-17 01:22:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!