Schedule-Creator

Java GUI to create schedules for UNCG students
git clone git://git.wrycode.com/wrycode/archive/Schedule-Creator.git
Log | Files | Refs | README

commit 67ac5f06cf96533fd77f3bd4094381bc66ff9286
parent b37ce501388db89df61b5aa21c0362ebbe5ec606
Author: wrycode <wry@mm.st>
Date:   Thu, 20 Feb 2020 14:29:53 -0500

Merge pull request #7 from cloud0x9/sfar

Better code in parsedata class, with added potential to create a CSV file for database.
Diffstat:
Msrc/ScheduleCreator/ParseData.java | 51++++++++++++++++++++++-----------------------------
1 file changed, 22 insertions(+), 29 deletions(-)

diff --git a/src/ScheduleCreator/ParseData.java b/src/ScheduleCreator/ParseData.java @@ -2,45 +2,38 @@ package ScheduleCreator; /** * This class parses through text file's using regex and outputs the results. - * Last updated: February 18, 2020 + * Last updated: February 20, 2020 * * @author Sfar */ import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.regex.*; public class ParseData { protected static void parse() throws IOException { + //Regular expression, inclues class name, time and day. + final String RegexExpression = "(\\b[A-Z]{3}\\b.\\b[0-9]{3}\\b.* [0-9]{2}\\b)|((?:(?:1[0-2]|0?[1-9]):(?:[0-5][0-9]) ?(?:[AaPp][Mm])).-.(?:(?:1[0-2]|0?[1-9]):(?:[0-5][0-9]) ?(?:[AaPp][Mm])))|(TR\\b|MW\\b|MWF\\b|M\\b|T\\\\b|W\\b|R\\b|F\\b)"; + //A example text file, that will be parsed. + final String FileName = "TestFile.txt"; - //Temporary string needed to parse text file, will be removed later. - String line; - - //this is the regex expression that grabs abbreviated class name with section, time, and day of the class - // ex. CSC 340-01, 5:30-6:45, T H (days) - Pattern regex = Pattern.compile("\\b[A-Z]{3}\\b.\\b[0-9]{3}\\b.* [0-9]{2}\\b|([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]|([AaPp][Mm])\\b|TR\\b|MW\\b|MWF\\b|M\\b|T\\b|W\\b|R\\b|F\\b/g"); - - //alternative regex for above, this one includes CRN codes. - //("[0-9]{5}|\b[A-Z]{3}\b.\b[0-9]{3}\b.* [0-9]{2}\b|([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]|([AaPp][Mm])\b|TR\b|MW\b|MWF\b|M\b|T\b|W\b|R\b|F\b"); - - // A FileReader, this is a hardcoded example, and should be changed later. - BufferedReader r = new BufferedReader(new FileReader("TestFile.txt")); - - Pattern regexx = Pattern.compile("\\b[A-Z]{3}\\b.\\b[0-9]{3}\\b.* [0-9]{2}\\b|([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]|([AaPp][Mm])\\b|TR\\b|MW\\b|MWF\\b|M\\b|T\\b|W\\b|R\\b|F\\b/g"); - - // matching lines with regex expression - while ((line = r.readLine()) != null) { - // find lines that match - Matcher regexMatcher = regex.matcher(line); - //loop through line by line to find matches, will be changed later. - while (regexMatcher.find()) { - // Get the starting position of the text - int start = regexMatcher.start(0); - // Get ending position - int end = regexMatcher.end(0); - // Print whatever matched. - System.out.println(line.substring(start, end)); - } + //Take content of txt file and put it into a string to make it easier to parse. + String content = new String(Files.readAllBytes(Paths.get(FileName)), StandardCharsets.UTF_8); + + //Give the matcher the both the text file and the regex expression so it can parse. + Matcher m = Pattern.compile(RegexExpression).matcher(content); + + //loop that prints matches. + while (m.find()) { + //print all matches, group(0) means everything. + System.out.println(m.group(0)); + + //The print statment below dosent work fully yet, but this would create a csv format of all the data + //System.out.println("{" +m.group(1) +" , "+ m.group(2) +" , "+ m.group(3) + "}"); } + } }