Finding and Matching
matches, find, lookingAt.
Finding and Matching is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Ways to Apply a Pattern
Matcher offers three core tests:
matches()requires the whole input to match.lookingAt()requires a match at the start.find()searches for a match anywhere.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("\\d+").matcher("42");
System.out.println(m.matches());
}
}matches: Whole Input
matches returns true only if the entire string conforms to the pattern, as if it were anchored at both ends.
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d+");
System.out.println(p.matcher("123").matches());
System.out.println(p.matcher("123abc").matches());
}
}lookingAt: Match at the Start
lookingAt succeeds if the input begins with a match, even if the rest does not match.
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d+");
System.out.println(p.matcher("123abc").lookingAt());
System.out.println(p.matcher("abc123").lookingAt());
}
}find: Match Anywhere
find scans forward for the next match. It returns true if one exists from the current position onward.
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d+");
System.out.println(p.matcher("abc123def").find());
}
}Iterating All Matches with find
Calling find in a loop walks through every match in the input.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("\\d+").matcher("a1bb22ccc333");
while (m.find()) {
System.out.println(m.group());
}
}
}Match Positions: start and end
After a successful match, start() and end() give the index range, and group() gives the matched text.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("\\d+").matcher("abc123");
if (m.find()) {
System.out.println(m.group() + " at [" + m.start() + "," + m.end() + ")");
}
}
}Anchors with find
Anchors ^ and $ let find behave like start/end checks. Here ^\d+ only matches digits at the very start.
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern p = Pattern.compile("^\\d+");
System.out.println(p.matcher("123abc").find());
System.out.println(p.matcher("abc123").find());
}
}Resetting a Matcher
reset() rewinds the matcher to the beginning so you can scan the same input again.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("a").matcher("aaa");
int first = 0;
while (m.find()) first++;
m.reset();
int second = 0;
while (m.find()) second++;
System.out.println(first + " " + second);
}
}Reusing a Matcher on New Input
reset(newInput) reuses the same matcher object with a different string, avoiding a fresh allocation.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("\\d+").matcher("abc");
m.reset("xyz789");
System.out.println(m.find());
System.out.println(m.group());
}
}matches vs find Summary
Choose by intent: use matches to validate a full value (like a whole ID), and find to extract or locate parts within larger text.
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern id = Pattern.compile("[A-Z]{2}\\d{4}");
System.out.println(id.matcher("AB1234").matches());
System.out.println(id.matcher("id=AB1234;").find());
}
}Counting Matches
A common task is counting occurrences. Loop find and increment a counter.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("o").matcher("foo boo zoo");
int count = 0;
while (m.find()) count++;
System.out.println(count);
}
}Quick Check
Which method requires the entire input string to match the pattern?
Recap
You located matches:
matchestests the whole input;lookingAttests the start;findsearches anywhere.- Loop
findto iterate all matches and readgroup,start,end. resetrewinds, andreset(input)reuses the matcher on new text.
Frequently asked questions
Is the “Finding and Matching” lesson free?
Yes — the full text of “Finding and Matching” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Finding and Matching”?
matches, find, lookingAt. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Finding and Matching” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Compiling Patterns
- Finding and Matching
- Capturing Groups
- Replacing with Regex