Last active
March 18, 2019 17:11
-
-
Save uvlad7/228daf12d1aa58fab95a8ccf895164af to your computer and use it in GitHub Desktop.
УП 4.2
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.application.Application; | |
import javafx.collections.FXCollections; | |
import javafx.geometry.HPos; | |
import javafx.geometry.VPos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.ListView; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.control.TitledPane; | |
import javafx.scene.image.Image; | |
import javafx.scene.layout.ColumnConstraints; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.RowConstraints; | |
import javafx.stage.Stage; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
public class Main extends Application { | |
public static void main(String[] args) { | |
Application.launch(args); | |
} | |
@Override | |
public void start(Stage stage) { | |
GridPane root = new GridPane(); | |
//root.setGridLinesVisible(true); | |
root.setPrefWidth(800); | |
root.setPrefHeight(120); | |
ColumnConstraints columnConstraints1 = new ColumnConstraints(); | |
columnConstraints1.setPercentWidth(70); | |
columnConstraints1.setHalignment(HPos.CENTER); | |
ColumnConstraints columnConstraints2 = new ColumnConstraints(); | |
columnConstraints2.setPercentWidth(30); | |
RowConstraints rowConstraints1 = new RowConstraints(); | |
rowConstraints1.setPrefHeight(Integer.MAX_VALUE); | |
rowConstraints1.setValignment(VPos.TOP); | |
RowConstraints rowConstraints2 = new RowConstraints(); | |
rowConstraints2.setPrefHeight(40); | |
RowConstraints rowConstraints3 = new RowConstraints(); | |
rowConstraints3.setMinHeight(5); | |
rowConstraints3.setMaxHeight(5); | |
rowConstraints1.setValignment(VPos.CENTER); | |
root.getColumnConstraints().addAll(columnConstraints1, columnConstraints2); | |
root.getRowConstraints().addAll(rowConstraints1, rowConstraints3, rowConstraints2, rowConstraints3); | |
TextArea textArea = new TextArea(); | |
textArea.setWrapText(true); | |
textArea.setText("2018-10-10 BC пять 9/11/1999 н.э. четыре 07.01.1111 до нашей эры 1999-09-11 три BC два 29.02.2018 раз"); | |
root.add(textArea, 0, 0); | |
ListView<String> listView = new ListView<>(); | |
TitledPane titledPane = new TitledPane("Dates", listView); | |
titledPane.setPrefHeight(Integer.MAX_VALUE); | |
titledPane.setCollapsible(false); | |
root.add(titledPane, 1, 0, 1, 4); | |
Button button = new Button("Parse"); | |
button.setPrefWidth(100); | |
root.add(button, 0, 2); | |
button.setOnAction(event -> listView.setItems(FXCollections.observableArrayList(Parser.parse(textArea.getText())))); | |
Scene scene = new Scene(root); | |
stage.setScene(scene); | |
stage.setTitle("Parsing"); | |
try (FileInputStream icon = new FileInputStream("src\\regexp.png")) { | |
stage.getIcons().add(new Image(icon)); | |
} catch (IOException e) { | |
} | |
stage.sizeToScene(); | |
stage.show(); | |
} | |
} |
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 java.util.ArrayList; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Parser { | |
private static Pattern datePattern = Pattern.compile("(?:(?:(?:(?:(?!0000)\\d{4})-" + | |
"(?:(?:(?:(?:0[1-9])|(?:1[0-2]))[-‐−‒–—](?:(?:0[1-9])|(?:1\\d)|(?:2[0-8])))|(?:(?:(?:0[13-9])|(?:1[0-2]))[-‐−‒–—](?:(?:29)|(?:30)))|(?:(?:(?:0[13578])|(?:1[02]))[-‐−‒–—]31)))" + | |
"|(?:(?:(?:(?:(?:[13579][26])|(?:[2468][048])|(?:0[48]))00)|(?:\\d{2}(?:(?:[13579][26])|(?:[2468][048])|(?:0[48]))))[-‐−‒–—]02[-‐−‒–—]29)" + | |
"|(?:(?:(?:(?:(?:0[1-9])|(?:1\\d)|(?:2[0-8]))\\.(?:(?:0[1-9])|(?:1[0-2])))|(?:(?:(?:29)|(?:30))\\.(?:(?:0[13-9])|(?:1[0-2])))|(?:31\\.(?:(?:0[13578])|(?:1[02]))))" + | |
"\\.(?:(?!0000)\\d{4}))" + | |
"|(?:(?:(?:(?:(?:0?[1-9])|(?:1[0-2]))/(?:(?:0?[1-9])|(?:1\\d)|(?:2[0-8])))|(?:(?:(?:0?[13-9])|(?:1[0-2]))/(?:(?:29)|(?:30)))|(?:(?:(?:0?[13578])|(?:1[02]))/31))" + | |
"/(?:(?!0000)\\d{4}))" + | |
"|(?:(?:(?:29\\.02\\.)|(?:0?2/29/))(?:(?:(?:(?:[13579][26])|(?:[2468][048])|(?:0[48]))00)|(?:\\d{2}(?:(?:[13579][26])|(?:[2468][048])|(?:0[48]))))))" + | |
"(?: ?(?:(?:[BA]C)|(?:(?:до )?(?:(?:н\\. ?э\\.)|(?:нашей эры)))))?)"); | |
public static List<String> parse(String text) { | |
List<String> list = new ArrayList<>(); | |
Matcher matcher = datePattern.matcher(text); | |
while (matcher.find()) { | |
list.add(matcher.group()); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment