In Java, sc.findInLine(pattern) searches forward from the current position only up to the next line separator for the next substring matching pattern, ignoring any token delimiters.
If a match is found, it advances past the match and returns the matched String. If no match appears on the rest of the current line, it returns null and leaves the scanner's position unchanged.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.findInLine(...).
Syntax
Pass a regex String or compiled Pattern to search anywhere within the current line.
Java
Scanner sc = new Scanner("Order #402 total $19.50");
String id = sc.findInLine("#\\d+"); // "#402"
String price = sc.findInLine("\\d+\\.\\d+"); // "19.50"
Call
What it does
sc.findInLine("#\\d+")
Finds the first match on the current line, ignoring spaces/delimiters.
sc.findInLine(pattern)
Same search using a precompiled Pattern.
sc.match()
Inspects capture groups from the last non-null findInLine() call.
Because findInLine() never crosses a line break, it stops when it reaches \n or \r\n. Call nextLine() to move onto the next line before searching again.
Library declarations
Java
// java.util.Scanner
public String findInLine(Pattern pattern)
public String findInLine(String pattern)
Parameters
Parameter
Description
pattern
A regex String or compiled Pattern to search for on the current line.
Return Value
Return Value
Description
String
The matched substring if found on the current line; otherwise null.
Throws IllegalStateException
If the scanner has been closed.
Throws PatternSyntaxException
If a String pattern contains invalid regex syntax.
Compare with Similar Methods
Searching without delimiters vs delimiter-based reads: