Print first letter of each word in a string using regex Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 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 { public static void main(String[] args) { String s1 = "Geeks for Geeks"; String s2 = "A Computer Science Portal for Geeks"; Pattern p = Pattern.compile("\\b[a-zA-Z]"); Matcher m1 = p.matcher(s1); Matcher m2 = p.matcher(s2); System.out.println("First letter of each word from string \"" + s1 + "\" : "); while (m1.find()) System.out.print(m1.group()); System.out.println(); System.out.println("First letter of each word from string \"" + s2 + "\" : "); while (m2.find()) System.out.print(m2.group()); } } C# // C# program to demonstrate extracting first // letter of each word using Regex using System; using System.Text.RegularExpressions; public class GFG { public static void Main() { string s1 = "Geeks for Geeks"; string s2 = "A Computer Science Portal for Geeks"; Regex p = new Regex("\\b[a-zA-Z]"); Console.WriteLine("First letter of each word from string \"" + s1 + "\" : "); foreach (Match ItemMatch in p.Matches(s1)) Console.Write(ItemMatch.Value); Console.WriteLine(); Console.WriteLine("First letter of each word from string \"" + s2 + "\" : "); foreach (Match ItemMatch in p.Matches(s2)) Console.Write(ItemMatch.Value); } } // This code is contributed by Utkarsh Python3 # Python program to demonstrate extracting first # letter of each word using Regex import re s1="Geeks for Geeks" s2="A Computer Science Portal for Geeks" p = re.compile("\\b[a-zA-Z]") print("First letter of each word from string '{0}' : ".format(s1)) for m1 in re.finditer(p,s1): print(m1.group(0),end="") print() print("First letter of each word from string '{0}' : ".format(s2)) for m2 in re.finditer(p,s2): print(m2.group(0),end="") # This code is contributed by Pushpesh Raj. Output: First letter of each word from string "Geeks for Geeks" : GfGFirst letter of each word from string "A Computer Science Portal for Geeks" : ACSPfGNext Article: Extracting each word from a String using Regex in Java If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Comment More info K kartik Improve Article Tags : Java Java-Strings java-regular-expression Java-String-Programs Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like