

- 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
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 Questions & Answers
- Write a Regular Expression to remove all special characters from a JavaScript String?
- How to Split String in Java using Regular Expression?
- 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
- How to match nonword characters in Python using Regular Expression?
- How to split on successions of newline characters using Python regular expression?
- Program to match vowels in a string using regular expression in Java
- 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 match at the end of string in python using Regular Expression?
- How to extract each (English) word from a string using regular expression in Java?
- How to print duplicate characters in a String using C#?
- Print all distinct characters of a string in order in C++
- How to use special characters in Python Regular Expression?
Advertisements