

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Regular Expression Metacharacter in Java
The subexpression/metacharacter “\t” matches the tab spaces.
Example 1
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\t"; Scanner sc = new Scanner(System.in); System.out.println("Enter a string: "); String input = sc.nextLine(); Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count ++; } System.out.println("Number of tab spaces: "+count); } }
Output
Enter a string: This is a sentence with tab spaces Number of tab spaces: 6
Example 2
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\t"; Scanner sc = new Scanner(System.in); System.out.println("Enter a string: "); String input = sc.nextLine(); Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; String result = ""; while(m.find()) { result = m.replaceAll(" "); } System.out.println("Result: "+result); } }
Output
Enter a string: This is a sentence with tab spaces Result: This is a sentence with tab spaces
- Related Questions & Answers
- Regular Expression Metacharacter in Java
- Regular Expression re+ Metacharacter in Java
- Regular Expression E Metacharacter in Java.
- Regular Expression Q Metacharacter in Java
- Regular Expression "" Metacharacter in Java
- Regular Expression re{ n} Metacharacter in Java
- Regular Expression a|b Metacharacter in Java
- Regular expression “[X?+] ” Metacharacter Java
- Regular Expression "^" (caret) Metacharacter in Java
- Regular Expression "$" (dollar) Metacharacter in Java
- Regular Expression "." (dot) Metacharacter in Java
- Regular Expression "re*" Metacharacter in Java
- Regular Expression "W" Metacharacter in Java
- Regular Expression "S" Metacharacter in Java
- Regular Expression "D" Metacharacter in Java
Advertisements