-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
110 lines (94 loc) · 3.23 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import org.joni.Regex;
import org.joni.Matcher;
import org.joni.Option;
import org.joni.exception.JOniException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
public class Main extends Application
{
private String computeResultStr(String regexStr, String txtToCompute) {
String result = "Result: none at the moment";
Regex regex;
try {
regex = new Regex(regexStr);
} catch (JOniException e) {
return "Result: Failed to parse textmate regex: " + e;
}
ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(CharBuffer.wrap(txtToCompute));
byte[] txtToComputeBytes = new byte[byteBuffer.remaining()];
byteBuffer.get(txtToComputeBytes);
Matcher matcher = regex.matcher(txtToComputeBytes);
try {
int matchIndex = matcher.search(0, 0, txtToComputeBytes.length, Option.NONE);
result = "Result: match index: " + matchIndex;
if (matchIndex == -1) {
result = "Result: no match";
} else {
result += "\n" + matcher.getEagerRegion();
}
}
catch (JOniException | ArrayIndexOutOfBoundsException e) {
result = "Result: Failed to match textmate regex: " + e;
}
return result;
}
@Override
public void start(Stage primaryStage) {
Label regexLabel, checkLabel, resultLabel;
TextField regexTxt, checkTxt;
Button button;
HBox hbox;
VBox regexVBox, checkVBox, sceneVBox;
Scene scene;
regexLabel = new Label("Type the RegEx:");
regexTxt = new TextField("");
regexTxt.setMaxWidth(200);
checkLabel = new Label("Type the text to check:");
checkTxt = new TextField("");
checkTxt.setMaxWidth(200);
resultLabel = new Label("Result: none at the moment");
regexTxt
.textProperty()
.addListener((observable, oldValue, newValue) -> {
resultLabel.setText(computeResultStr(newValue, checkTxt.getText()));
});
checkTxt
.textProperty()
.addListener((observable, oldValue, newValue) -> {
resultLabel.setText(computeResultStr(regexTxt.getText(), newValue));
});
regexVBox = new VBox(regexLabel, regexTxt);
regexVBox.setSpacing(5);
regexVBox.setAlignment(Pos.CENTER_LEFT);
checkVBox = new VBox(checkLabel, checkTxt);
checkVBox.setSpacing(5);
checkVBox.setAlignment(Pos.CENTER_LEFT);
hbox = new HBox();
hbox.getChildren().addAll(regexVBox, checkVBox);
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
HBox.setMargin(hbox, new Insets(10, 0, 0, 10));
sceneVBox = new VBox();
sceneVBox.getChildren().addAll(hbox, resultLabel);
sceneVBox.setSpacing(20);
sceneVBox.setAlignment(Pos.CENTER);
scene = new Scene(sceneVBox, 500, 200);
primaryStage.setTitle("jruby/joni regex sandbox");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// launch(args);
}
}