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("Match 1: " + Pattern.compile("s.*e").matcher(s).find()); System.out.println("Match 2: " + Pattern.compile("\\bs.*e\\b").matcher(s).find()); System.out.println("Match 3: " + Pattern.compile("s...e").matcher(s).find()); System.out.println("Match 4: " + Pattern.compile("b.d").matcher(s).find()); List<String> subgroups = Utils.findFirst(Pattern.compile("(\\w+)\\s*(\\w+)"), s); System.out.println("Find First (with subgroups): " + Utils.listToString(subgroups)); subgroups = Utils.findFirst(Pattern.compile("bad match"), s); System.out.println("Find First (bad match): " + Utils.listToString(subgroups)); List<String> matches = Utils.findAll(Pattern.compile("\\w+"), s); System.out.println("Find All: (matches only)" + Utils.listToString(matches)); matches = Utils.findAll(Pattern.compile("bad match"), s); System.out.println("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("Replace First 1: " + Pattern.compile("\\.\\.\\.").matcher(s).replaceFirst("!!!")); System.out.println("Replace First 2: " + Pattern.compile("\\b...\\b").matcher(s).replaceFirst("???")); System.out.println("Replace First 3: " + Pattern.compile("b.d").matcher(s).replaceFirst("???")); System.out.println("Replace First 4: " + Pattern.compile("(\\w+) (\\w+)").matcher(s).replaceFirst("$2 $1")); System.out.println("Replace All 1: " + Pattern.compile("\\b...\\b").matcher(s).replaceAll("???")); System.out.println("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("Split 1: " + Utils.listToString(Arrays.asList(Pattern.compile(" ").split(str)))); System.out.println("Split 2: " + Utils.listToString(Arrays.asList(Pattern.compile("[eo]").split(str)))); System.out.println("Split 3: " + Utils.listToString(Arrays.asList(Pattern.compile("\\s").split(str)))); System.out.println("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]]