
- 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 get first letter of each word using regular expression in Java?
The metacharacter “\b” matches for the word boundaries and [a-zA-Z] matches one character from the English alphabet (both cases). In short, the expression \\b[a-zA-Z] matches one single character from the English alphabet, both cases after every word boundary.
Therefore, to retrieve the first letter of each word −
Compile the above expression of the compile() method of the Pattern class.
Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class.
Finally, for each match get the matched characters by invoking the group() method.
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class FirstLetterExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter sample text: "); String data = sc.nextLine(); String regex = "\b[a-zA-Z]"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Creating a Matcher object Matcher matcher = pattern.matcher(data); System.out.println("First letter of each word from the given string: "); while(matcher.find()) { System.out.print(matcher.group()+" "); } } }
Output
Enter sample text: National Intelligence Agency Research & Analysis Wing First letter of each word from the given string: N I A R A W
- Related Articles
- Java Program to Print first letter of each word using regex
- How to extract each (English) word from a string using regular expression in Java?
- Getting first letter of each word in a String using regex in Java
- C++ Program to Print first letter of each word using regex
- Golang program to print first letter of each word using regex
- How to capitalize the first letter of each word in a string using JavaScript?
- Capitalizing first letter of each word JavaScript
- Print first letter of each word in a string using C# regex
- How to match a word in python using Regular Expression?
- Capitalize last letter and Lowercase first letter of a word in Java
- Print first letter of each word in a string in C#
- Python program to capitalize each word's first letter
- How to Split String in Java using Regular Expression?
- How to match digits using Java Regular Expression (RegEx)
- How to print the first character of each word in a String in Java?

Advertisements