frankie3552
frankie3552

Reputation: 23

Matcher/Pattern not printing

import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class 3HourLabHours {

    public static void main(String[] args) throws FileNotFoundException {
       //create string variable for filepath
    String fileName = "C:\\Users\\Frank\\Downloads\\Courses.txt";

    String line = null;

    try {

            FileReader fileReader = new FileReader(fileName);
        //scans file into bufferedReader
            BufferedReader bufferedReader = new BufferedReader(fileReader);
        //goes through each line in text file
            while ((line = bufferedReader.readLine()) != null) {
        //creates pattern to match
             Pattern p = Pattern.compile("((^[a-zA-Z]+\\s\\d+[a-zAz]\\s\\d\\s\\d\\s)(\\d))");
        //matches pattern to line in text                                                              
                Matcher m = p.matcher(line);

                if (m.find()) {   
                      System.out.print(m); 
                }
            }
        }catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

I'm wondering why the pattern I created won't match any of the lines in the text. The bold is what is supposed to be matched and printed.

text file example:

"Accounting ACG 2021 3 3 0 PRINCIPLES OF FINANCIAL ACCOUNTING • Prerequisite: An assessment score for placement in MAT 0024C or higher mathematics or a minimum grade of C in MAT 0012C or in APA 1111 or in MTB 1103 Accounting concepts, principles, procedures and underlying theories applicable to nature of accounting, financial statements, accounting cycle, current assets, plant and equipment, long-term investments, intangible assets, payroll, current liabilities, long-term debt, and owner's equity including accounting for sole proprietorships, partnerships, and corporations. (Special Fee: $30.00) ACG 2071 3 3 0 PRINCIPLES OF MANAGERIAL ACCOUNTING • Prerequisite: ACG 2021 Accounting for business information requirements with cost accounting concepts and relationships, statement of cash flows, financial statement analysis, cost-volume-profit analysis, variance analysis, budgeting, pricing decisions, capital expenditure decisions, and management accounting analysis for decision-making. (Special Fee: $30.00) ACG 2100 3 3 0 INTERMEDIATE ACCOUNTING I"

Upvotes: 0

Views: 51

Answers (1)

Ti Strga
Ti Strga

Reputation: 1390

Try removing the "^" from the pattern and see if that changes the results.

The caret means that the pattern will only match at the beginning of input. Since you're matching a line at a time, the boldface text would have to be at the start of a line to match.

Edit: also, the "[a-zAz]" is not going to work. First, you probably typo'd the range of letters. Also, none of the bold text has digits followed by a required single letter. (Double edit: what 4castle said in the comments.)

Edit #3: Remove the "3" from the start of your class name. Type names can't start with a number in Java.

Upvotes: 1

Related Questions