
- 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
How to print all the characters of a string using regular expression in Java?
The meta character "." matches all the characters, to print all the characters using the regular expressions −
Compile the regular expression using the compile() method.
Create a Matcher object using the matcher() method.
Find the matches using the find() method and for every match print the matched contents (characters) using the group() method.
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { //Regular expression to match a string of non-word with length 2 to 6 String regex = "."; Scanner sc = new Scanner(System.in); System.out.println("Enter your input string: "); String input = sc.nextLine(); //Creating a Pattern object Pattern p = Pattern.compile(regex); //Creating a Matcher object Matcher m = p.matcher(input); while(m.find()) { System.out.println(m.group()); } } }
Output
Enter your input string: This is a sample text T h i s i s a s a m p l e t e x t
- Related Articles
- How to Split String in Java using Regular Expression?
- Write a Regular Expression to remove all special characters from a JavaScript String?
- Moving all special char to the end of the String using Java Regular Expression RegEx)
- Java Program to split a string using Regular Expression
- Find all the numbers in a string using regular expression in Python
- Program to match vowels in a string using regular expression in Java
- How to extract each (English) word from a string using regular expression in Java?
- How to split on successions of newline characters using Python regular expression?
- How to print duplicate characters in a String using C#?
- Print all permutation of a string using ArrayList in Java
- How to match at the beginning of string in python using Regular Expression?
- How to use special characters in Python Regular Expression?
- Print all distinct characters of a string in order in C++
- How to match digits using Java Regular Expression (RegEx)
- Java Regular expression to check if a string contains alphabet

Advertisements