Created
November 20, 2018 08:07
-
-
Save hatimmohammed369/9fd6ace4db2ae46ae788754b61dcffb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.stage.Stage; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.control.Label; | |
import javafx.scene.text.Font; | |
import javafx.scene.control.Button; | |
import javafx.stage.FileChooser; | |
import javafx.scene.Scene; | |
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.util.ArrayList; | |
public class RegexMatcher extends javafx.application.Application{ | |
static final TextArea regexArea = new TextArea(); | |
static final TextArea inputArea = new TextArea(); | |
@Override | |
public void start(Stage primaryStage){ | |
primaryStage.setOnCloseRequest((event) -> System.exit(0)); | |
Pane root = new Pane(); | |
Label regexLabel = new Label("Regex:"); | |
regexLabel.setFont(Font.font(15)); | |
Label inputLabel = new Label("Input:"); | |
inputLabel.setFont(Font.font(15)); | |
Button match = new Button("Match"); | |
match.setFont(Font.font(15)); | |
match.setPrefSize(200, 40); | |
match.setOnAction((event) -> { | |
final String regex = regexArea.getText(); | |
final String input = inputArea.getText(); | |
if(regex.equals("") || input.equals("")) | |
return; | |
File matchesFile = new File( "matches.txt" ); | |
if(matchesFile.exists()) | |
matchesFile.delete(); | |
try(FileWriter writer = new FileWriter(matchesFile)){ | |
for(String e : match(regex, input)) | |
writer.write(e+"\r\n"); | |
}catch(Exception e){ | |
System.out.println(e); | |
} | |
}); | |
Button loadRegexFromFile = new Button("Load regex from file"); | |
loadRegexFromFile.setFont(Font.font(15)); | |
loadRegexFromFile.setPrefSize(200, 40); | |
loadRegexFromFile.setOnAction((event) -> { | |
FileChooser fileChooser = new FileChooser(); | |
fileChooser.setTitle("Open regex file"); | |
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Files", "*.*")); | |
fileChooser.setInitialDirectory(new File(".")); | |
File selectedFile = fileChooser.showOpenDialog(primaryStage); | |
try{ | |
if (selectedFile != null) | |
regexArea.setText(readFile(selectedFile)); | |
}catch(Exception e){ | |
System.out.println(e); | |
} | |
}); | |
Button loadInputFromFile = new Button("Load input from file"); | |
loadInputFromFile.setFont(Font.font(15)); | |
loadInputFromFile.setPrefSize(200, 40); | |
loadInputFromFile.setOnAction((event) -> { | |
FileChooser fileChooser = new FileChooser(); | |
fileChooser.setTitle("Open input file"); | |
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Files", "*.*")); | |
fileChooser.setInitialDirectory(new File(".")); | |
File selectedFile = fileChooser.showOpenDialog(primaryStage); | |
try{ | |
if (selectedFile != null) | |
inputArea.setText(readFile(selectedFile)); | |
}catch(Exception e){ | |
System.out.println(e); | |
} | |
}); | |
root.getChildren().addAll(regexLabel, regexArea, loadRegexFromFile, inputLabel, inputArea, loadInputFromFile, match); | |
regexLabel.relocate(10, 20); | |
regexArea.relocate(10, 45); | |
loadRegexFromFile.relocate(500, 110); | |
inputLabel.relocate(10, 240); | |
inputArea.relocate(10, 265); | |
loadInputFromFile.relocate(500, 330); | |
match.relocate(140, 470); | |
primaryStage.setScene(new Scene(root, 720, 530)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) throws Exception{ | |
mylaunch(args); | |
} | |
public static void mylaunch(String ... args) throws Exception{ | |
if(args.length == 0){ | |
launch(args); | |
}else if(args.length == 1){ | |
if(args[0].equalsIgnoreCase("-gui")){ | |
launch(args); | |
}else if(args[0].equalsIgnoreCase("-help") || args[0].equalsIgnoreCase("-h")){ | |
System.out.println("Usage: java Application [options] [values]"); | |
System.out.println("Note:The first argument right after options is the regex (pattern), "+ | |
"the second one is the input"); | |
System.out.println("\t-gui: luanches the application in GUI mode"); | |
System.out.println("Note: -gui option comes the first one in options"); | |
System.out.println("\t-text: indicate that either regex or input is simple text passed through arguments"); | |
System.out.println("\t\texample: java Application -text -text \"\\d+\" \"input 100\""); | |
System.out.println("\t-text: indicate that either regex or input is a file"); | |
System.out.println("\t\texample: java Application -file -file \"regex.txt\" \"input.txt\""); | |
System.exit(0); | |
} | |
}else if(args.length == 4 || args.length == 5){ | |
String textValue = args[args.length-1]; | |
String regexValue = args[args.length-2]; | |
String regex = (contains("-gui", args, false) ? | |
(args[1].equalsIgnoreCase("-text") ? args[3] : readFile(args[3])) : | |
(args[0].equalsIgnoreCase("-text") ? args[2] : readFile(args[2])) ); | |
String input = (contains("-gui", args, false) ? | |
(args[2].equalsIgnoreCase("-text") ? args[4] : readFile(args[4])) : | |
(args[1].equalsIgnoreCase("-text") ? args[3] : readFile(args[3])) ); | |
if(contains("-gui", args, false)){ | |
regexArea.setText(regex); | |
inputArea.setText(input); | |
launch(args); | |
}else{ | |
File matchesFile = new File("matches.txt"); | |
try(FileWriter writer = new FileWriter(matchesFile)){ | |
for(String e : match(regex, input)) | |
writer.write(e+"\r\n"); | |
}catch(Exception e){ | |
System.out.println(e); | |
} | |
System.exit(0); | |
} | |
} | |
System.out.println("Invalid arguments"); | |
System.exit(0); | |
} | |
static String readFile(File file) throws Exception{ | |
String data = ""; | |
try(FileReader reader = new FileReader( file )){ | |
int c = 0; | |
while((c = reader.read()) != -1) | |
data += (char)c; | |
}catch(Exception e){ | |
System.out.println(e); | |
} | |
return data; | |
} | |
static String readFile(String fileName) throws Exception{ | |
return readFile(new File(fileName)); | |
} | |
static int indexOf(String elem, String[] a, boolean caseSensitive){ | |
for(int i = 0;i < a.length;i++) | |
if(caseSensitive){ | |
if(a[i].equals(elem)) | |
return i; | |
}else{ | |
if(a[i].equalsIgnoreCase(elem)) | |
return i; | |
} | |
return -1; | |
} | |
static int lastIndexOf(String elem, String[] a, boolean caseSensitive){ | |
for(int i = a.length-1;i >= 0;i--) | |
if(caseSensitive){ | |
if(a[i].equals(elem)) | |
return i; | |
}else{ | |
if(a[i].equalsIgnoreCase(elem)) | |
return i; | |
} | |
return -1; | |
} | |
static boolean contains(String elem, String[] a, boolean caseSensitive){ | |
return (indexOf(elem, a, caseSensitive) != -1); | |
} | |
static ArrayList<String> match(String pattern, String input) { | |
ArrayList<String> matches = new ArrayList<>(); | |
java.util.regex.Matcher m = (Pattern.compile(pattern)).matcher(input); | |
int count = 0; | |
while(m.find()) { | |
count++; | |
matches.add("group(" + count + "): " | |
+ m.group()); | |
} | |
return matches; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment