Get the first letter of each word in a string using regex in Java Last Updated : 13 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string, extract the first letter of each word in it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Geeks for geeks Output :Gfg Input : United Kingdom Output : UK Below is the Regular expression to extract the first letter of each word. It uses '/b'(one of boundary matchers). Please refer How to write Regular Expressions? to learn it. \b[a-zA-Z] Java // Java program to demonstrate extracting first // letter of each word using Regex import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { static void printFirst(String s) { Pattern p = Pattern.compile("\\b[a-zA-Z]"); Matcher m = p.matcher(s); while (m.find()) System.out.print(m.group()); System.out.println(); } public static void main(String[] args) { String s1 = "Geeks for Geeks"; String s2 = "A Computer Science Portal for Geeks"; printFirst(s1); printFirst(s2); } } OutputGfG ACSPfG Comment More infoAdvertise with us Next Article Check if a String Contains only Alphabets in Java using Regex K kartik Improve Article Tags : Java java-regular-expression Java-String-Programs Practice Tags : Java Similar Reads Print first letter of each word in a string using regex Given a string, extract the first letter of each word in it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Geeks for geeksOutput :Gfg Input : United KingdomOutput : UKBelow is the Regular expression to extract 3 min read Extracting each word from a String using Regex in Java Given a string, extract words from it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Funny?? are not you? Output : Funny are not you Input : Geeks for geeks?? Output : Geeks for geeks We have discussed a solut 2 min read Check if a String Contains only Alphabets in Java using Regex Regular Expressions or Regex is an API for defining String patterns that can be used for searching, manipulating, and editing a text. It is widely used to define a constraint on strings such as a password. Regular Expressions are provided under java.util.regex package. For any string, here the task 2 min read Reverse words in a given String in Java Let's see an approach to reverse words of a given String in Java without using any of the String library function Examples: Input : "Welcome to geeksforgeeks" Output : "geeksforgeeks to Welcome" Input : "I love Java Programming" Output :"Programming Java love I" Prerequisite: Regular Expression in J 3 min read Searching For Characters and Substring in a String in Java Efficient String manipulation is very important in Java programming especially when working with text-based data. In this article, we will explore essential methods like indexOf(), contains(), and startsWith() to search characters and substrings within strings in Java.Searching for a Character in a 5 min read Java Program to Get a Character from a String Given a String str, the task is to get a specific character from that String at a specific index. Examples:Input: str = "Geeks", index = 2Output: eInput: str = "GeeksForGeeks", index = 5Output: F Below are various ways to do so: Using String.charAt() method: Get the string and the indexGet the speci 5 min read Like