53905

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

Question:

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.. || */

Answer1:

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

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

<a href="https://regex101.com/r/gN3eJ9/1" rel="nofollow">RegEx Demo</a>

Answer2:

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()); }

Recommend

  • reading char array of number more than 10
  • Want to fetch data of user using id in linkedin using rest api
  • data.table roll join not working correctly
  • Selenium: Error comes using “if” to decide whether an element exists or not
  • concurrent.futures issue: why only 1 worker?
  • Mongo Index not being used
  • Why is OpenCV Gpu module performing faster than VisionWorks?
  • Django __unicode__ and FK is very slow
  • Ejabberd custom IQ Handler: getting feature-not-implemented or service-unavailable
  • JavaScript RegEx to match punctuation NOT part of any HTML tags
  • JavaScript equivalent of the MySQL function SUBSTRING_INDEX()
  • Multiple tags with same authentication manager in Spring Security 3.1
  • Can i run WebAppContext with WebSocketHandler in same instance?
  • Increasing dimensions on hover without changing the position of other elements
  • overloading the extraction operator >> in C++ [duplicate]
  • java.lang.NullPointerException: No FileItemFactory has been set
  • How to do custom filtering in Datatables with comma separated values?
  • problem with calling libraries
  • Sorting a HashMap, while keeping duplicates
  • Delphi: Form becomes Frozen while assigning strings in thread
  • Prevent focus to URL bar with CTRL + L
  • Need faster concat of columns in sql server table
  • Correct implementation of List Iterator methods
  • Best practice to eliminate magic numbers within a member function
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • Ensure fsync did its job
  • Alert pop up with LWUIT
  • Matrix multiplication with MKL
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • File not found error Google Drive API
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?