find in line java code example

Example: find in line java

// Java program to illustrate the 
// findInLine() method of Scanner class in Java 

import java.util.*; 
import java.util.regex.Pattern; 

public class GFG1 { 
	public static void main(String[] argv) 
		throws Exception 
	{ 

		try { 

			// Get the string to be searched 
			String s = "Geeksforgeeks has Scanner Class Methods"; 

			// Print the string 
			System.out.println("Target String:\n" + s); 

			// create a new scanner 
			// with the specified String Object 
			Scanner scanner = new Scanner(s); 

			// finds a pattern of any 5 letter plus "for" 
			System.out.println("\nAny 5 letter plus for : "
							+ scanner.findInLine( 
									Pattern.compile(".....for"))); 

			// close the scanner 
			scanner.close(); 
		} 
		catch (IllegalStateException e) { 
			System.out.println("Exception thrown : " + e); 
		} 
	} 
}

Tags:

Misc Example