
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Matching Nonprintable Characters using Java regex
There are 7 common non printable characters used in general and each character has its own hexadecimal representation.
Name | characters | Hexa-decimal representation |
---|---|---|
bell | \a | 0x07 |
Escape | \e | 0x1B |
Form feed | \f | 0x0C |
Line feed | \n | 0x0A |
Carriage return | \r | 0X0D |
Horizontal tab | \t | 0X09 |
Vertical tab | \v | 0X0B |
Example 1
Following Java program accepts an input text and counts the number of tab spaces in it −
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "\t"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); int count =0; while (matcher.find()) { count++; } System.out.println("Number of tab spaces in the given iput text: "+count); } }
Output
sample text with tab spaces Number of tab spaces in the given input text: 3
Example 2
You can also use the respective hexa-decimal representations of the non-printable characters to match.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "\x09"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); int count =0; while (matcher.find()) { count++; } System.out.println("Number of tab spaces in the given iput text: "+count); } }
Output
Enter input text: sample data with tab spaces Number of tab spaces in the given input text: 4
- Related Articles
- How to match word characters using Java RegEx?
- How to match a range of characters using Java regex
- How to match a fixed set of characters using Java RegEx
- Matching from a set of characters Java regualr expression
- Pattern matching in C# with Regex
- Pattern matching in Python with Regex
- How to match the regex meta characters in java as literal characters.
- Matching strings for similar characters - JavaScript
- Count the Number of matching characters in a pair of Java string
- Date format validation using Java Regex
- How to get last 2 characters from string in C# using Regex?
- Python Program to check if String contains only Defined Characters using Regex
- Regex metacharacters in Java Regex
- JavaScript regex - How to replace special characters?
- How to return all matching strings against a RegEx in JavaScript?

Advertisements