From 8ee5a15a31aecf6131d80e2ae05e2ae7c7ce3e1e Mon Sep 17 00:00:00 2001 From: Hammy Date: Fri, 25 Dec 2020 06:02:04 +0000 Subject: [PATCH] Experimenting with Regex --- .../src/string/formatting/Main.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/string/formatting/Main.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/string/formatting/Main.java index 191edcb..6b52548 100644 --- a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/string/formatting/Main.java +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/string/formatting/Main.java @@ -6,6 +6,8 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.Formatter; import java.util.StringJoiner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Main { private static final String rootResources = "C:\\Users\\sgoud\\JavaProjects\\Learning-Java\\Pluralsite\\src\\intermediate\\Java Fundamentals - The Core Platform\\resources\\"; @@ -18,6 +20,8 @@ public class Main { formattingStrings(); writeFormattedContentToFile(); + System.out.println(); + useRegex(); } private static void stringJoiner() { @@ -123,4 +127,24 @@ public class Main { formatter.format("My nephews are %d, %d, %d and %d years old", david, dawson, dillon, gordon); } } + + private static void useRegex() { + String test = "apple, apple and orange please"; + + System.out.println(test.replaceAll("ple", "ricot")); + System.out.println(test.replaceAll("ple\\b", "ricot")); + + String[] parts = test.split("\\b"); + for (String part : parts) { + if (part.matches("\\w+")) { + System.out.println(part); + } + } + + Pattern pattern = Pattern.compile("\\w+"); + Matcher matcher = pattern.matcher(test); + while (matcher.find()) { + System.out.println(matcher.group()); + } + } }