如何防止特殊字符后的文本存储-在Java的arraylist中 [英] How to prevent storing of text coming after special characters -- in arraylist in java
问题描述
我正在从文本文件中读取pl/sql代码,并将其所有单词从下面的代码存储到数组列表中:
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
对于上面的代码,我不想在列表中存储注释(-这是一个dummy.table)和(--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.. ||
*/
推荐答案
要删除内联注释,您是否考虑过使用indexOf()
和substring()
的简单组合?另外,您应该使用in1.nextLine()
而不是in1.next()
吗?例如:
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());
}
这篇关于如何防止特殊字符后的文本存储-在Java的arraylist中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!