Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

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

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..               ||
*/
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add part after -- till end of line in your regex like this:

in1.useDelimiter("/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|--[^\n]*|[\p{javaWhitespace}\.|,]+");

RegEx Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...