HIDE NAV

JavaFX Searching Text Using RegEx

This program shows how to use a Pattern defined by Regular Expressions to create a Matcher object which will locate return matches as groups. Input file is located by prompting the user to save a file with a file-browser like dialog using JavaFX FileChooser.

JFXTextSearch.java


package ceccs;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

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

public class JfxTextSearchDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox root = new VBox();

        Button browseBtn = new Button("Browse");
        Label outLabel = new Label();
        root.getChildren().addAll(browseBtn,outLabel);

        browseBtn.setOnAction(event -> {
            FileChooser fc = new FileChooser();
            File chosenFile = fc.showOpenDialog(primaryStage);
            try {

                Scanner fileScanner = new Scanner(chosenFile);

                String entireFileContents = new String();
                while(fileScanner.hasNextLine()){
                    String line = fileScanner.nextLine();
                    entireFileContents += line + "\n";
                }

                int vowelCount = getVowelCount(entireFileContents);
                int charCount = getCharCountNoRegEx(entireFileContents);
                int wordCount = getWordCountBeginsWithLetter(entireFileContents);
                outLabel.setText("vowel count = " + vowelCount + "\n"
                        + "character count = " + charCount + "\n"
                        + "word count = " + wordCount);

            } catch (Exception ex){
                outLabel.setText("File Error!");
            }
        });

        Scene scn = new Scene(root, 300, 200);
        primaryStage.setScene(scn);
        primaryStage.show();


    }

    public static void main(String[] args) {
        launch(args);
    }

    static int getVowelCount (String s){
        int count = 0;
        Pattern pattern = Pattern.compile("([aeiouyAEIOUY])");
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()){
            count++;
            System.out.println("match " + count + " " + matcher.group());
        }
        return count;
    }

    static int getWordCountBeginsWithLetter (String s){
        int count = 0;
        Pattern pattern = Pattern.compile("([a-zA-Z]\\w+)");
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()){
            count++;
            String word =  matcher.group();
            System.out.println("match " + count + " '" + word + "' length = " + word.length());
        }
        return count;
    }

    static int getCharCountNoRegEx(String s){
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) count++;
        }
        return count;
    }
}