|
| 1 | +package week2.intro; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Paths; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class RegexExercise { |
| 9 | + |
| 10 | + // sõnad "kalad" või "jalad". |
| 11 | + public static final String RE1 = "[kj]alad"; |
| 12 | + |
| 13 | + // Viietähelised sõned, mis lõppevad tähtedega "alad". |
| 14 | + public static final String RE2 = ".alad"; // sõnad täpsemalt: \b\walad\b |
| 15 | + |
| 16 | + // color ja colour |
| 17 | + public static final String RE3 = "colou?r"; |
| 18 | + |
| 19 | + // jaha, jahaaaaaaaaa! |
| 20 | + public static final String RE4 = "ja+ha+"; |
| 21 | + |
| 22 | + // binaarsõned |
| 23 | + public static final String RE5 = "^[01]*$"; |
| 24 | + |
| 25 | + // eelviimane täht on "a" |
| 26 | + public static final String RE6 = "^[ab]*a[ab]$"; |
| 27 | + |
| 28 | + // tagasiviited |
| 29 | + public static final String RE7 = "^(..)\\1$"; |
| 30 | + |
| 31 | + // nimede asendamine: regulaaravaldis |
| 32 | + public static final String RE8 = "^(\\w*) (\\w*)$"; |
| 33 | + // millega asendada? (Util.replace'i teine argument) |
| 34 | + public static final String RP8 = "$2, $1"; |
| 35 | + |
| 36 | + // Eemaldada sulud! |
| 37 | + public static final String RE9 = " \\(.*?\\)"; |
| 38 | + // Siin asendatakse regexiga sobituvad juppid tühja sõnega. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + // Testimise meetod, et saaks natuke debuugida, muidu on ka automaattestid. |
| 43 | + |
| 44 | + static void main() throws IOException { |
| 45 | + List<String> strings = Files.readAllLines(Paths.get("inputs", "regex.txt")); |
| 46 | + printLines(RegexUtils.grep(RE1, strings)); |
| 47 | + } |
| 48 | + |
| 49 | + private static void printLines(List<String> strings) { |
| 50 | + for (String s : strings) System.out.println(s); |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments