Hyroko
  • Hyroko
How do you make a regular expression ignore all characters that exist inside of double quotation marks and comment braces? For example, in the statement String apple = "Orange"; having the regex output String apple And in this example String apple; //this is an apple string. The regex would output String apple
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
woodrow73
  • woodrow73
for ignoring quotes, you could break down the line/program into a char array, and recreate the String ignoring any "....", or something of the kind. Ignoring comments is easy if you can first isolate each line individually from eachother, find the position of "//", and delete everything afterwards with the substring method.. or something.
woodrow73
  • woodrow73
you should repost that with the code enclosed in 3 ``` ontop and on bottom
Hyroko
  • Hyroko
``` package hashtable; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author Tony Fighter */ public class HashTable { String[] theArray; String[] reserved; String[] names; int[] locations; int arraySize; String [] reservedList = {"abstract","assert","boolean","break","byte" ,"case","catch","char","class","const","continue","default","do" ,"double","else","enum","extends","final","finally","float","for" ,"goto","if","implements","import","instanceof","int","interface" ,"long","native","new","package","private","protected","public" ,"return","short","strictfp","super","switch","synchronized","this" ,"throw","throws","transient","try","void","volatile","while","false" ,"null","true"}; /** * @param args the command line arguments */ public static void main(String[] args) { } //constructor for the hash table. HashTable(int size){ arraySize = size; theArray = new String[size]; Arrays.fill(theArray, "☺");//fills the array with smileys as holders. } /** *hashes words into either the reserved table or the names table. * * @param stringsForArray an array of words to hash. */ public void theHashFunction(String[] stringsForArray){ for (String newElement : stringsForArray) { if(isReserved(newElement)){ hashReserved(newElement); } else{ hashName(newElement); } } } //tests to see if the word passed to it is in the reserved list. private boolean isReserved(String newElement) { return Arrays.asList(reservedList).contains(newElement); } //stores a reserved word into the reserved hash table. //if there are multiples of the same word, ignore the duplicates. private void hashReserved(String newElement) { throw new UnsupportedOperationException("Not supported yet."); } //stores a legal name into the name hash table. //if there are multiples of the same word, ignore the duplicates. private void hashName(String newElement) { } //searches the original file for a word, and returns an array of all //line locations of the provided word. private void nameLocations(String[] names, Scanner scan){ String str; int i = 0; int count = 0; int lineNumber = 1; ArrayList instances; while(scan.hasNextLine()){ Pattern p = Pattern.compile(names[i]); Matcher m = p.matcher(scan.nextLine()); i++; while(m.find()){ count++; } if(count>=1){ } lineNumber++; } } //reads a line from a file and removes all unwanted chars. private String parseLineFromFile (Scanner scan) throws IOException{ String str; StringBuilder stuff = new StringBuilder(); str = scan.nextLine(); while(str.length() != 0){ int x = str.charAt(0); if(//Need REGEX here to test for all valid names that are //not comments or inside strings ){ char valid = (char)x; stuff.append(valid); str = str.substring(1); } else{ str = str.substring(1); } } str = stuff.toString(); return str; } //takes a full string of characters, including periods and spaces, //and splts them up according to white space. private String[] separate(String input){ String[] words = input.split(" "); return words; } //Reads the file, calls other methods to create an array //of all the strings found in the file private String[] getArray(Scanner scan){ String str; ArrayList temp = new ArrayList(); String[] finishedArray = null; boolean done = false; while(!done){ if(scan.hasNextLine()){ try { String[] words = separate(parseLineFromFile(scan)); temp.addAll(Arrays.asList(words)); } catch (IOException ex) { Logger.getLogger(HashTable.class.getName()).log(Level.SEVERE, null, ex); } }else{ done = true; finishedArray = (String[]) temp.toArray(); } } return finishedArray; } } ``` Ok gotcha.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

Hyroko
  • Hyroko
This is the section in question ``` //reads a line from a file and removes all unwanted chars. private String parseLineFromFile (Scanner scan) throws IOException{ String str; StringBuilder stuff = new StringBuilder(); str = scan.nextLine(); while(str.length() != 0){ int x = str.charAt(0); if(//Need REGEX here to test for all valid names that are //not comments or inside strings ){ char valid = (char)x; stuff.append(valid); str = str.substring(1); } else{ str = str.substring(1); } } ```
Hyroko
  • Hyroko
I was originally testing for individual chars, without regex to find all legal chars used for names. but I realize I needed a much more complicated regex to ignore comments and strings, then test to see if the values I get back after that are valid names or are one of the 53 reserved words in java, then return an array of all the words found.
woodrow73
  • woodrow73
So you basically need to, given a String, 1. remove comments 2. remove quotations 3. extract reserve words into array (or ArrayList) ? correct me if I'm missing something
woodrow73
  • woodrow73
*not quotations but strings
Hyroko
  • Hyroko
Yes, I will be reading a java file (in plain text) into the program line by line, and hashing all reserved words found into a hash table, and all the legal names into another hash table. after that, I will do a cross-listing of the file with the names hash table to generate a list of all the locations of the names in the file, if there are multiple of the same name in the file, it will still say so. Here is the page for the project I am doing. http://emunix.emich.edu/~haynes/311/fa15/Projects/pp5-hash.pdf
Hyroko
  • Hyroko
There will be no cross-listing of the reserved words, only the java names.
Hyroko
  • Hyroko
I'm passing all the valid names and reserved words into an array, so that I can have it pass it to the other methods to do the sorting and hashing.
woodrow73
  • woodrow73
so, as a summary, what exactly do you need help with?
Hyroko
  • Hyroko
I know how to regex for all valid chars used in names, but I am not entirely sure how to exclude those comments and strings. right now, I'm thinking [A-za-z0-9._$] for all my characters needed that are valid for names. I was using something like this ``` private String parseLineFromFile (Scanner scan) throws IOException{ String str; StringBuilder stuff = new StringBuilder(); str = scan.nextLine(); while(str.length() != 0){ int x = str.charAt(0); if( x >= 65 && x <= 122 || x == 32|| x == 46){ char valid = (char)x; stuff.append(valid); str = str.substring(1); } else{ str = str.substring(1); } } str = stuff.toString(); return str; } ``` but I forgot numbers and $ and _ are also legal for names (I'm not too worried about the ones other than that) also, periods are being included too, So passing something like Java.util.*; should output into an array [Java.util.] and String $my_homie = "yo my homie"; should output the array ["String" , "$my_homie"]
Hyroko
  • Hyroko
forgot the quotation marks in that first example, but you get the gist I hope.
Hyroko
  • Hyroko
My getArray method calls the separate method which calls the parseLinefromFile method. Lol parseLineFromFile takes the line from the text file, and stores it as an array, with all the unwanted bits and portions gone. then separator turns the string returned from it into a string array by splitting the string at each white space. then the getArray method repeats this process until the whole file is read in. Adding each line array to an arrayList, then finally turning the arrayList back into a regular array. that array it used by the theHashFunction method to hash the whole array into both the reserved and name tables.
woodrow73
  • woodrow73
Alright, I'd recommend removing comments and Strings prior to your already made regexing algorithm I don't know how precise you need to be with it.. here's a quick example that removes comments without checking whether it's inside of a String ``` public String removeComment(String str) { boolean hasComment = false; int commentPos = -1; for(int i = 0; i < str.length()-1; i++) { if(str.charAt(i) == '/' && str.charAt(i+1) == '/') { hasComment = true; commentPos = i; } } if(hasComment) { return str.substring(0, commentPos); } else { return str; } } ``` note untested method
woodrow73
  • woodrow73
By finding the first instance of "//", it wont have to worry about other instances of "//" later
Hyroko
  • Hyroko
yes, I'll have to tweak it a little to deal with block comments like ``` /** * * * @param newElement */ ``` but thank you that will be helpful. I can figure it out from there. Good suggestion.
woodrow73
  • woodrow73
Sure, and with a method to delete strings, I would recommend looping through all characters in a String, and constantly add each character to a new String. And when you first find a quote (str.charAt(i) == '\"' I think), turn on a boolean called deleteMode, and when deleteMode is on, do not add any characters to the new String. Just call deleteMode = !deleteMode when you find a quote.. something like that should work.
Hyroko
  • Hyroko
thanks a lot, you've been a great help
woodrow73
  • woodrow73
Sure, remember nothing is impossible in programming; many ways to skin a cat.

Looking for something else?

Not the answer you are looking for? Search for more explanations.