In Java, sc.findWithinHorizon(pattern, horizon) searches forward up to horizon characters—across line boundaries and ignoring delimiters—for the next match of pattern.
Passing horizon = 0 removes the character limit so the scanner searches all remaining input until a match is found or EOF is reached.
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.findWithinHorizon(...).
Syntax
Pass a regex String or Pattern together with a non-negative character limit (0 means unlimited).
Searches across all remaining lines (0 = no limit).
sc.findWithinHorizon(pattern, 50)
Searches at most 50 characters ahead from the current cursor.
sc.match()
Inspects match offsets and capture groups after a non-null result.
If a match starts inside the horizon window or extends past horizon characters from the current cursor, it is not matched until the window covers the full pattern.
Library declarations
Java
// java.util.Scanner
public String findWithinHorizon(Pattern pattern, int horizon)
public String findWithinHorizon(String pattern, int horizon)
Parameters
Parameter
Description
pattern
Regex String or compiled Pattern to locate.
horizon
Maximum characters to inspect (0 for unlimited; negative values throw).
Return Value
Return Value
Description
String
The matched substring (advancing past it), or null if not found within the horizon.
Throws IllegalArgumentException
If horizon < 0.
Throws IllegalStateException
If the scanner is closed.
Compare with Similar Methods
Cross-line pattern search vs single-line and anchored skips: