Regular Expressions

This page is under construction. Please come back later.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import org.pureprogrammer.Utils;
public class RegEx1 {
public static void main(String[] args) {
final String s = "Four score and seven years ago...";
System.out.println(Utils.join("", "Match 1: ", Pattern.compile("s.*e").matcher(s).find()));
System.out.println(Utils.join("", "Match 2: ", Pattern.compile("\\bs.*e\\b").matcher(s).find()));
System.out.println(Utils.join("", "Match 3: ", Pattern.compile("s...e").matcher(s).find()));
System.out.println(Utils.join("", "Match 4: ", Pattern.compile("b.d").matcher(s).find()));
List<String> subgroups = Utils.findFirst(Pattern.compile("(\\w+)\\s*(\\w+)"), s);
System.out.println(Utils.join("", "Find First (with subgroups): ", Utils.listToString(subgroups)));
subgroups = Utils.findFirst(Pattern.compile("bad match"), s);
System.out.println(Utils.join("", "Find First (bad match): ", Utils.listToString(subgroups)));
List<String> matches = Utils.findAll(Pattern.compile("\\w+"), s);
System.out.println(Utils.join("", "Find All: (matches only)", Utils.listToString(matches)));
matches = Utils.findAll(Pattern.compile("bad match"), s);
System.out.println(Utils.join("", "Find All (bad match): ", Utils.listToString(matches)));
}
}
Output
$ javac -Xlint RegEx1.java
$ java -ea RegEx1
Match 1: true
Match 2: true
Match 3: true
Match 4: false
Find First (with subgroups): ["Four score", "Four", "score"]
Find First (bad match): []
Find All: (matches only)["Four", "score", "and", "seven", "years", "ago"]
Find All (bad match): []
import java.util.regex.Pattern;
import org.pureprogrammer.Utils;
public class RegEx2 {
public static void main(String[] args) {
final String s = "Four score and seven years ago...";
System.out.println(Utils.join("", "Replace First 1: ", Pattern.compile("\\.\\.\\.").matcher(s).replaceFirst("!!!")));
System.out.println(Utils.join("", "Replace First 2: ", Pattern.compile("\\b...\\b").matcher(s).replaceFirst("???")));
System.out.println(Utils.join("", "Replace First 3: ", Pattern.compile("b.d").matcher(s).replaceFirst("???")));
System.out.println(Utils.join("", "Replace First 4: ", Pattern.compile("(\\w+) (\\w+)").matcher(s).replaceFirst("$2 $1")));
System.out.println(Utils.join("", "Replace All 1: ", Pattern.compile("\\b...\\b").matcher(s).replaceAll("???")));
System.out.println(Utils.join("", "Replace All 2: ", Pattern.compile("(\\w+) (\\w+)").matcher(s).replaceAll("$2 $1")));
}
}
Output
$ javac -Xlint RegEx2.java
$ java -ea RegEx2
Replace First 1: Four score and seven years ago!!!
Replace First 2: Four score ??? seven years ago...
Replace First 3: Four score and seven years ago...
Replace First 4: score Four and seven years ago...
Replace All 1: Four score ??? seven years ???...
Replace All 2: score Four seven and ago years...
import java.util.Arrays;
import java.util.regex.Pattern;
import org.pureprogrammer.Utils;
public class RegEx3 {
public static void main(String[] args) {
final String STR = "Four score and seven years ago...";
System.out.println(Utils.join("", "Split 1: ", Utils.listToString(Arrays.asList(Pattern.compile(" ").split(STR)))));
System.out.println(Utils.join("", "Split 2: ", Utils.listToString(Arrays.asList(Pattern.compile("[eo]").split(STR)))));
System.out.println(Utils.join("", "Split 3: ", Utils.listToString(Arrays.asList(Pattern.compile("\\s").split(STR)))));
System.out.println(Utils.join("", "Split 4: ", Utils.listToString(Arrays.asList(Pattern.compile("\\W").split(STR)))));
}
}
Output
$ javac -Xlint RegEx3.java
$ java -ea RegEx3
Split 1: ["Four", "score", "and", "seven", "years", "ago..."]
Split 2: ["F", "ur sc", "r", " and s", "v", "n y", "ars ag", "..."]
Split 3: ["Four", "score", "and", "seven", "years", "ago..."]
Split 4: ["Four", "score", "and", "seven", "years", "ago"]
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Java Language Specification]], Java SE 17 Edition, Gosling, et. al., 2021.
- [[Java Tutorials]]
- [[Java at TutorialsPoint]]
- Download Java at [[Amazon Corretto]], [[Azul Zulu]], [[Eclipse Temurin]] or [[Oracle JDK]]
Pure Programmer


