English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Subexpression/Meta-character " \ A ”Matches the beginning of the entire string.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\\AHi"; String input = "Hi how are you welcome to w"3codebox"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: ");+count); } }
Output result
Number of matches: 1
The following Java program accepts a string from the user to verify if it contains non-ASCII characters.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StartingOfInput { public static void main( String args[] ) { String regex = \\\\A\\p{ASCII}*\\(z"; Scanner sc = new Scanner(System.in); System.out.println("Enter the input string: "); String input = sc.nextLine(); //Create a Pattern object Pattern p = Pattern.compile(regex); //Create a Matcher object Matcher m = p.matcher(input); if(m.find()) { System.out.println("Given input contains only ASCII characters "); } else { System.out.println("Given input contains non-ASCII characters "); } } }
Enter the input string: What is your name Given input contains only ASCII characters
Enter the input string: why do we fall Given input contains non-ASCII characters